2016-06-23 118 views
0

我试图挖掘的形象图,并打开默认照片查看器中的形象:Android设备上查看图像意图不显示图像

void handleOpenImage(){ 
    try { 
     File temp = File.createTempFile("myImage", ".png"); 
     BitmapDrawable bitmapDrawable = (BitmapDrawable) attachedImageView.getDrawable(); 
     Bitmap bitmap = bitmapDrawable.getBitmap(); 
     FileOutputStream stream = new FileOutputStream(temp); 
     boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream); 
     if(!success){ 
      throw new Exception(); 
     } 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(temp), "image/*"); 
     startActivity(intent); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

然而,当我调用该函数,画廊活动开始,但不显示我的图像。图像文件在临时路径中成功创建,并且双击检查。为什么不是这个意图工作?

+0

代替startActivity(意向)使用这一个,检查它是否在我其他应用程序开放, startActivity(Intent.createChooser(意向,“选择图片”)); – Vickyexpert

+0

@Vickyexpert哦抱歉刚刚得到你的意思。忽略我以前的评论。会尝试。 –

+0

@Vickyexpert尝试了它,我得到了同样的结果:我选择图库,它会打开,但图片不显示。我剩下一个空的画廊活动 –

回答

1

正如我在评论中提到的那样,temp文件的绝对路径为/data/data/MY_APPS_IDENTIFIER/cache/ulouder-1004534880.png。 出于安全原因,此路径位于每个应用程序具有的私人空间中,并且无法由其他应用程序访问。

通过将temp文件保存到其他位置,图库应用可以访问它并正确显示图像。