user975869
user975869

Reputation: 285

Android: image from assets saved as bitmap is corrupted

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

Upvotes: 0

Views: 2552

Answers (1)

user975869
user975869

Reputation: 285

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");

Upvotes: 1

Related Questions