启动 Intent.ACTION_VIEW 意图不适用于保存的图像文件 [英] Launching Intent.ACTION_VIEW intent not working on saved image file

查看:16
本文介绍了启动 Intent.ACTION_VIEW 意图不适用于保存的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我说这个问题与我的另一个问题.实际上它是为此而创建的.

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

我有以下代码将从网上下载的位图写入sd卡中的文件:

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

一些注意事项.在看到有人使用它后,我正在创建可变"位图,它似乎比没有它工作得更好.我在 Uri 类上使用 parse 方法而不是 fromFile 因为在我的代码中我在不同的地方调用这些方法当我创建意图时我有一个字符串路径而不是文件.

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.

现在是我的问题.文件被创建.该意图启动一个对话框,要求我选择一个查看器.我安装了 3 个查看器.Astro 图​​像查看器,默认媒体库(我在 2.1 上有一个 milstone,但在里程碑上,2.1 更新不包括 3d 库,所以它是旧的)和来自 nexus 的 3d 库(我在野生).

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).

现在,当我启动 3 个查看器时,会发生以下情况:

Now when i launch the 3 viewers the following happen:

  • Astro 图​​像查看器: 活动发射,但我只看到一个黑屏.

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

媒体库:我得到一个例外对话框显示应用程序媒体画廊(过程com.motorola.gallery)已停止不料.请再试一次"与强制关闭选项.

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 画廊:一切正常应该.

当我尝试使用 Astro 文件管理器简单地打开文件(浏览到它并单击)时,我得到相同的选项对话框,但这次情况有所不同:

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 图​​片查看器:一切正常应该的.

媒体库:一切正常应该.

3D 画廊:活动启动但我只看到黑屏.

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?

正如评论中所指出的,这里是 adb logcat 中感兴趣的部分.另外我应该注意到我改变了我创建图像文件的方式.因为我想创建一个反映在线文件的文件,所以我只需下载它而不是创建位图然后创建文件(这样做是因为在某些时候我需要位图,但现在我反过来做了).问题仍然存在,并且完全相同:

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):启动活动:意图{行为=android.intent.action.VIEWdat=/sdcard/Android/data/com.myapp/files/image.jpgtyp=image/* flg=0x3800000cmp=com.motorola.gallery/.ViewImage }

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):启动过程com.motorola.gallery:ViewImage for活动com.motorola.gallery/.ViewImage:pid=29187 uid=10017 gids={3003, 1015}

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):调试器线程不活动,忽略 DDM 发送(t=0x41504e4d l=38)

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

I/dalvikvm(29187):调试器线程不活动,忽略 DDM 发送(t=0x41504e4d l=64)

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

I/ActivityManager(18852):进程com.handcent.nextsms (pid 29174) 有死了.

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

I/ViewImage(29187):在视图中onCreate!

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

D/AndroidRuntime(29187):正在关闭虚拟机

D/AndroidRuntime(29187): Shutting down VM

W/dalvikvm(29187):threadid=3:线程以未捕获的异常退出(组=0x4001b170)

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

E/AndroidRuntime(29187):未捕获处理程序:由于线程主退出未捕获异常

E/AndroidRuntime(29187): Uncaught handler: thread main exiting due to uncaught exception

E/AndroidRuntime(29187):java.lang.RuntimeException:无法开始活动组件信息{com.motorola.gallery/com.motorola.gallery.ViewImage}:java.lang.NullPointerException

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

E/AndroidRuntime(29187):在java.lang.reflect.Method.invokeNative(原生方法)

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

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

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

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

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

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

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

E/AndroidRuntime(29187):在dalvik.system.NativeStart.main(本机方法)

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

E/AndroidRuntime(29187):由:java.lang.NullPointerException

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

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

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

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

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

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

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

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

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

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

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

E/AndroidRuntime(29187):……还有 11 个

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

推荐答案

我使用这个 hack 来修复错误:

I used this hack to fix error:

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

这篇关于启动 Intent.ACTION_VIEW 意图不适用于保存的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆