Running Intent.ACTION_VIEW does not work on saved image file

First of all, let me say that these questions are slightly related to another question . In fact, it was created because of this.

I have the following code to write a bitmap downloaded from the network to a file on the SD card:

// Get image from url URL u = new URL(url); HttpGet httpRequest = new HttpGet(u.toURI()); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); InputStream instream = bufHttpEntity.getContent(); Bitmap bmImg = BitmapFactory.decodeStream(instream); instream.close(); // Write image to a file in sd card File posterFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/data/com.myapp/files/image.jpg"); posterFile.createNewFile(); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(posterFile)); Bitmap mutable = Bitmap.createScaledBitmap(bmImg,bmImg.getWidth(),bmImg.getHeight(),true); mutable.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); // Launch default viewer for the file Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(posterFile.getAbsolutePath()),"image/*"); ((Activity) getContext()).startActivity(intent); 

A few notes. I create a โ€œmutableโ€ bitmap when I see someone using it, and it seems to work better than without it. And I use the parse method for the Uri class, not fromFile, because in my code I call them in different places, and when I create the intent, I have a string path, not a file.

Now for my problem. The file is created. Target launches a dialog box asking you to select a viewer. I have 3 viewers installed. Astro Image Viewer, the default media gallery (I have milestone on 2.1, but at the milestone update 2.1 did not include a three-dimensional gallery, so it's old) and a 3d gallery from the nexus (I found apk in the wild).

Now, when I launch 3 viewers, the following happens:

  • Astro Image Viewer: Activity but I donโ€™t see anything but a black screen.

  • Media Gallery: I get an exception dialog box "Media Gallery application (process com.motorola.gallery) stopped unexpectedly. Try again" using the force close option.

  • 3D Gallery: Everything works as it should.

When I try to just open the file using the Astro file manager (go to it and just click), I get the same options dialog, but this time it's different:

  • Astro Image Viewer: Everything works as it should.

  • Media Gallery: Everything works as it should.

  • 3D Gallery: Activity starts, but I see only a black screen.

As you can see, everything is in complete disarray. I have no idea why this happens, but it happens every time. This is not an accidental error.

Am I missing something when I create an intention? Or when I create an image file? Any ideas?

EDIT: As noted in the comment, this is part of the interest in adb logcat. I should also note that I have changed the way I create the image file. Since I want to create a file that reflects an online file, I just upload it and not create a Bitmap and then create a file (this was done because at some point I need a bitmap, but now I do it the other way around). the problems persist and seem the same:

I / ActivityManager (18852): launch activity: Intent {act = android.intent.action.VIEW dat = / sdcard / android / data / com.myapp / files / image.jpg typ = image / * flg = 0x3800000 cmp = com .motorola.gallery / .ViewImage}

I / ActivityManager (18852): launch proc com.motorola.gallery:ViewImage for Event com.motorola.gallery/.ViewImage: pid = 29187 uid = 10017 gids = {3003, 1015}

I / dalvikvm (29187): debugger thread is not active, ignoring DDM transmission (t = 0x41504e4d l = 38)

I / dalvikvm (29187): debugger thread is not active, ignoring DDM transmission (t = 0x41504e4d l = 64)

I / ActivityManager (18852): process com.handcent.nextsms (pid 29174) has died.

I / ViewImage (29187): In View Image OnCreate!

D / AndroidRuntime (29187): shutting down the VM

W / dalvikvm (29187): threadid = 3: thread exit with an uncaught exception (Group = 0x4001b170)

E / AndroidRuntime (29187): Unopened handler: thread exiting due to an uncaught exception

E / AndroidRuntime (29187): java.lang.RuntimeException: unable to start activity ComponentInfo {com.motorola.gallery/com.motorola.gallery.ViewImage}: java.lang.NullPointerException

E / AndroidRuntime (29187): when android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2496)

E / AndroidRuntime (29187): when android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2512)

E / AndroidRuntime (29187): at android.app.ActivityThread.access $ 2200 (ActivityThread.java:119)

E / AndroidRuntime (29187): when android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1863)

E / AndroidRuntime (29187): with android.os.Handler.dispatchMessage (Handler.java:99)

E / AndroidRuntime (29187): when android.os.Looper.loop (Looper.java:123)

E / AndroidRuntime (29187): when android.app.ActivityThread.main (ActivityThread.java:4363)

E / AndroidRuntime (29187): with java.lang.reflect.Method.invokeNative (Native Method)

E / AndroidRuntime (29187): with java.lang.reflect.Method.invoke (Method.javaโˆ—21)

E / AndroidRuntime (29187): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:860)

E / AndroidRuntime (29187): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:618)

E / AndroidRuntime (29187): with dalvik.system.NativeStart.main (Native Method)

E / AndroidRuntime (29187): caused by: java.lang.NullPointerException

E / AndroidRuntime (29187): at com.motorola.gallery.ImageManager.allImages (ImageManager.java:5621)

E / AndroidRuntime (29187): at com.motorola.gallery.ImageManager.getSingleImageListByUri (ImageManager.java:5515)

E / AndroidRuntime (29187): at com.motorola.gallery.ViewImage.onCreate (ViewImage.java:1801)

E / AndroidRuntime (29187): when android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047)

E / AndroidRuntime (29187): when android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2459)

E / AndroidRuntime (29187): ... 11 more

+6
java android android-intent bitmap
source share
4 answers

I used this hack to fix the error:

 ... Uri hacked_uri = Uri.parse("file://" + uri.getPath()); intent.setDataAndType(hacked_uri, "image/*"); ... 
+33
source share

I used this code for my application and it works fine. I just changed litle.

I changed this:

 intent.setDataAndType(Uri.parse(posterFile.getAbsolutePath()),"image/*"); 

For this:

 intent.setDataAndType(Uri.fromFile(posterFile),"image/*"); 
+4
source share

I decided to create my own activity, which simply draws an image on the screen. This is not an ideal solution, but it meets my basic standards ... It works :)

+2
source share

I am using this code

  MediaScannerConnection.scanFile(PayamKhosusiActivity.this, new String[] { filez.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted(String path, Uri uri) { Log.wtf("onScanCompleted", "yes"); Intent intent = new Intent(Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setDataAndType(uri, "image/*"); intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); //must for reading data from directory startActivity(intent); } }); 
0
source share

All Articles