Android:来自保存为位图的资源的图像已损坏

时间:2011-12-09 14:21:14

标签: android file bitmap android-intent save

我正在尝试从资产中获取图像(由于图片数量较多而无法绘制的资源具有子文件夹)并希望使用ACTION_VIEW意图打开它。由于我不知道任何其他方式我试图将图像作为位图,保存为缓存作为jpg文件,打开并删除它关闭时。 (还有其他方法吗?)

不幸的是,它保存了一个文件,但它是corruped图像文件,模拟器打开黑色图像,手机显示错误图像,计算机说图像文件已损坏。我的代码有什么不好吗?

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
AssetManager mngr = getAssets();
                try {
                    InputStream is2 = mngr.open(type+"/"+type+""+mRowId+".jpg");
                    Bitmap bitmap = BitmapFactory.decodeStream(is2);
                    image.setImageBitmap(bitmap);
                    OutputStream outStream = null;
                    File file = new File(ShowActivity.this.getCacheDir(), type+""+mRowId+".jpg");
                    outStream = new FileOutputStream(file);


                      boolean compressed = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outStream);
                       outStream.flush();
                       outStream.close();


                } catch (final IOException e) {
                    e.printStackTrace();
                }

                String patht = ShowActivity.this.getCacheDir().getName() + File.separatorChar+type+""+mRowId+".jpg";
                intent.setDataAndType(Uri.fromFile(new File(patht)), "image/jpg");
                startActivity(intent);

编辑:

似乎代码正在运行,但我在logcat中收到以下错误:

12-09 15:17:02.568: INFO/ActivityManager(61): Starting: Intent                    { 
act=android.intent.action.VIEW dat=file:///cache/12.jpg typ=image/jpeg cmp=com.android.gallery/com.android.camera.ViewImage } from pid 378
12-09 15:17:02.728: ERROR/UriImage(389): got exception decoding bitmap
12-09 15:17:02.728: ERROR/UriImage(389): java.lang.NullPointerException
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.Util.makeInputStream(Util.java:336)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.Util.makeBitmap(Util.java:307)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.Util.makeBitmap(Util.java:299)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:94)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:86)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.gallery.UriImage.thumbBitmap(UriImage.java:120)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.ImageGetter$ImageGetterRunnable.executeRequest(ImageGetter.java:173)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.ImageGetter$ImageGetterRunnable.run(ImageGetter.java:149)
12-09 15:17:02.728: ERROR/UriImage(389):     at java.lang.Thread.run(Thread.java:1019)
12-09 15:17:02.728: ERROR/UriImage(389): got exception decoding bitmap 
12-09 15:17:02.728: ERROR/UriImage(389): java.lang.NullPointerException
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.Util.makeInputStream(Util.java:336)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.Util.makeBitmap(Util.java:307)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.Util.makeBitmap(Util.java:299)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.gallery.UriImage.fullSizeBitmap(UriImage.java:94)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.ImageGetter$ImageGetterRunnable.executeRequest(ImageGetter.java:204)
12-09 15:17:02.728: ERROR/UriImage(389):     at com.android.camera.ImageGetter$ImageGetterRunnable.run(ImageGetter.java:149)
12-09 15:17:02.728: ERROR/UriImage(389):     at java.lang.Thread.run(Thread.java:1019)
12-09 15:17:03.717: INFO/ActivityManager(61): Displayed com.android.gallery/com.android.camera.ViewImage: +1s58ms

1 个答案:

答案 0 :(得分:1)

有了它的工作,保存到SD没有任何区别。事实证明代码没问题,但是FilOutputStream创建了具有ACTION_VIEW无法打开的默认权限的文件。将FileOutputStream更改为openFileOutput有助于:

InputStream is2 = mngr.open(type+"/"+type+""+mRowId+".jpg");
                    Log.v(TAG, type+" "+mRowId);
                    Bitmap bitmap = BitmapFactory.decodeStream(is2);
                    FileOutputStream outStream = openFileOutput(type+""+mRowId+"cache.jpg", MODE_WORLD_READABLE);
                    boolean compressed = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outStream);
                    jpg = getFileStreamPath(type+""+mRowId+"cache.jpg");
                    outStream.close();

...

                 intent.setDataAndType(Uri.fromFile(jpg), "image/jpeg");