Android:保存为位图的资源中的图像已损坏
我正在尝试从资产中获取图像(由于图片数量较多,资产不可绘制以具有子文件夹),并希望以 ACTION_VIEW 意图打开它。因为我不知道任何其他方式,我试图将图像作为位图,保存为 jpg 文件缓存,打开并在关闭时删除。 (还有其他方法吗?)
不幸的是,它保存了一个文件,但它是损坏的图像文件,模拟器打开黑色图像,手机显示错误图像,计算机说图像文件已损坏。我的代码有什么不好的地方吗?
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
i'm trying to take an image from assets (assets not drawable to have subfolders due to high amount of pictures) and want it to be opened with an ACTION_VIEW intent. Since i don't know any other way i'm trying to take the image as a bitmap, save to cache as a jpg file, open and delete when its closed. (Is there any other way?)
Unfortunately, it saves a file, but it is corruped image file, emulator opens black image, phone shows error image, computer says the image file is corrupted. Anything bad in my code?
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);
EDIT:
seems like the code is working but i get the following error in 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
成功了,保存到 SD 没有什么区别。事实证明代码没问题,但 FilOutputStream 使用默认权限创建了 ACTION_VIEW 无法打开的文件。将 FileOutputStream 更改为 openFileOutput 有助于:
...
Got it work, saving to sd made no difference. It turned out the code was ok, but FilOutputStream creates the file with default permissions which ACTION_VIEW can`t open. Changing FileOutputStream to openFileOutput helped:
...