VIEW intent not working on saved image file

First of all let me say that this questions is slightly connected to another question by me. Actually it was created because of that.

I have the following code to write a bitmap downloaded from the net to a file in 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 am creating the "mutable" bitmap after seeing someone using it and it seems to work better than without it. And i am using the parse method on the Uri class and not the fromFile because in my code i am calling these in different places and when i am creating the intent i have a string path instead of a file.

Now for my problem. The file gets created. The intent launches a dialog asking me to select a viewer. I have 3 viewers installed. The Astro image viewer, the default media gallery (i have a milstone on 2.1 but on the milestone the 2.1 update did not include the 3d gallery so it's the old one) and the 3d gallery from the nexus one (i found the apk in the wild).

Now when i launch the 3 viewers the following happen:

  • Astro image viewer: The activity launches but i see nothing but a black screen.

  • Media Gallery: i get an exception dialog shown "The application Media Gallery (process com.motorola.gallery) has stopped unexpectedly. Please try again" with a force close option.

  • 3D gallery: Everything works as it should.

  • When i try to simply open the file using the Astro file manager (browse to it and simply click) i get the same option dialog but this time things are different:

  • Astro image viewer: Everything works as it should.

  • Media Gallery: Everything works as it should.

  • 3D gallery: The activity launches but i see nothing but a black screen.

  • As you can see everything is a complete mess. I have no idea why this happens but it happens like this every single time. It's not a random bug.

    Am i missing something when i am creating the intent? Or when i am creating the image file? Any ideas?

    EDIT: As noted in the comment here is the part of interest in adb logcat. Also i should note that i changed the way i create the image file. Since i want to create a file that reflects an online file i simply download it instead of creating a Bitmap and then creating the file (this was done because at some point i needed the Bitmap but now i do it the other way around). the problems persist thought and are exactly the same :

    I/ActivityManager(18852): Starting 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): Start proc com.motorola.gallery:ViewImage for activity com.motorola.gallery/.ViewImage: pid=29187 uid=10017 gids={3003, 1015}

    I/dalvikvm(29187): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=38)

    I/dalvikvm(29187): Debugger thread not active, ignoring DDM send (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 VM

    W/dalvikvm(29187): threadid=3: thread exiting with uncaught exception (group=0x4001b170)

    E/AndroidRuntime(29187): Uncaught handler: thread main exiting due to 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): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

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

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

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

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

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

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

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

    E/AndroidRuntime(29187): at java.lang.reflect.Method.invoke(Method.java:521)

    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): at 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): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

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

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


    我用这个黑客修复错误:

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

    I used this code for my app and works fine. I only did a litle change.

    I changed This:

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

    For this:

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

    I decided to create my own Activity which simply draws the image on the screen. It is not a perfect solution but it meets my basic standards... It works :)

    链接地址: http://www.djcxy.com/p/36742.html

    上一篇: 以编程方式设置ImageView宽度和高度?

    下一篇: VIEW意图无法处理保存的图像文件