从图库android中的缓存查看图像文件

时间:2016-04-12 14:50:20

标签: java android image caching gallery

我想将缓存的图片查看到图库中。我已经下载了Bitmap并缓存在临时文件

File cacheFile = File.createTempFile("Image", "jpg", context.getCacheDir());
FileOutputStream fos = new FileOutputStream(cacheFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();

然后我用了一个意图在画廊中查看它

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(cacheFile ), "image/*");
startActivity(intent);

我收到了以下异常

04-12 10:42:56.790 2281-2281/system:ui E/StrictMode: file:// Uri exposed through Intent.getData()
    java.lang.Throwable: file:// Uri exposed through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1580)
    at android.net.Uri.checkFileUriExposed(Uri.java:2339)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:7012)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1418)
    at android.app.Activity.startActivityForResult(Activity.java:3390)
    at android.app.Activity.startActivityForResult(Activity.java:3351)
    at android.app.Activity.startActivity(Activity.java:3587)
    at android.app.Activity.startActivity(Activity.java:3555)
    at com.android.internal.app.ResolverActivity.onIntentSelected(ResolverActivity.java:371)
    at com.android.internal.app.ResolverActivity.startSelected(ResolverActivity.java:285)
    at com.android.internal.app.ResolverActivity.onButtonClick(ResolverActivity.java:278)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at android.view.View$1.onClick(View.java:3628)
    at android.view.View.performClick(View.java:4240)
    at android.view.View$PerformClick.run(View.java:17721)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
04-12 10:42:56.900 2714-10842/com.android.gallery E/UriImage: got exception decoding bitmap 
    java.lang.NullPointerException
    at com.android.camera.Util.makeInputStream(Util.java:336)
    at com.android.camera.Util.makeBitmap(Util.java:307)
    at com.android.camera.Util.makeBitmap(Util.java:299)
    at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:94)
    at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:86)
    at com.android.camera.gallery.UriImage.thumbBitmap(UriImage.java:120)
    at com.android.camera.ImageGetter$ImageGetterRunnable.executeRequest(ImageGetter.java:173)
    at com.android.camera.ImageGetter$ImageGetterRunnable.run(ImageGetter.java:149)
    at java.lang.Thread.run(Thread.java:841)
04-12 10:42:56.900 2714-10842/com.android.gallery E/UriImage: got exception decoding bitmap 
    java.lang.NullPointerException
    at com.android.camera.Util.makeInputStream(Util.java:336)
    at com.android.camera.Util.makeBitmap(Util.java:307)
    at com.android.camera.Util.makeBitmap(Util.java:299)
    at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:94)
    at com.android.camera.ImageGetter$ImageGetterRunnable.executeRequest(ImageGetter.java:204)
    at com.android.camera.ImageGetter$ImageGetterRunnable.run(ImageGetter.java:149)
    at java.lang.Thread.run(Thread.java:841)

有什么建议吗? 的更新
不知怎的,我现在没有例外,但仍然没有工作。

1 个答案:

答案 0 :(得分:1)

后缀应为.jpg.jpeg,而不仅仅是jpg

  

后缀 - 用于生成文件名称的后缀字符串;可以为null,在这种情况下后缀为" .tmp"将被使用

修改

画廊无法访问路径,因为它是您应用的tmp目录。您必须将图片放在外面(外部存储可能?),以便图库可以访问它。