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

发布于 2024-12-20 21:39:35 字数 3778 浏览 2 评论 0原文

我正在尝试从资产中获取图像(由于图片数量较多,资产不可绘制以具有子文件夹),并希望以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-12-27 21:39:35

成功了,保存到 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");

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:

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文