启动 Intent.ACTION_VIEW 意图不适用于保存的图像文件

发布于 2024-09-04 14:22:17 字数 5363 浏览 4 评论 0原文

首先,我要说的是,这个问题与 另一个问题略有关联< /a> 由我。事实上它是因此而创建的。

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

// 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,因为在我的代码中,我在不同的地方调用这些方法,并且当我创建意图时,我有一个字符串路径而不是文件。

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

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

  • Astro 图​​像查看器: 活动 发射了,但我什么也没看到,除了一个 黑屏。

  • 媒体库:我遇到了例外 显示的对话框“应用程序媒体 画廊(过程 com.motorola.gallery)已停止 不料。请再试一次” 强制关闭选项。

  • 3D 画廊:一切正常 应该。

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

  • Astro 图​​像查看器: 一切正常 正如它应该的那样。

  • 媒体库:一切正常 应该。

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

正如你所看到的,一切都一团糟。我不知道为什么会发生这种情况,但每次都会这样。这不是一个随机错误。

当我创建意图时我是否遗漏了一些东西?或者当我创建图像文件时?有什么想法吗?

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

I/ActivityManager(18852):正在启动 活动:意图{ act=android.intent.action.VIEW dat=/sdcard/Android/data/com.myapp/files/image.jpg 典型=图像/* flg=0x3800000 cmp=com.motorola.gallery/.ViewImage }

I/ActivityManager(18852):启动进程 com.motorola.gallery:查看图片 活动 com.motorola.gallery/.ViewImage: pid=29187 uid=10017 gids={3003, 1015}

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

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

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

I/ViewImage(29187):在查看图像中 onCreate!

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

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

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

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

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

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

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

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

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

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

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

E/AndroidRuntime(29187):位于 java.lang.reflect.Method.invokeNative(本机 方法)

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

E/AndroidRuntime(29187):位于 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):位于 dalvik.system.NativeStart.main(本机 方法)

E/AndroidRuntime(29187):原因是: java.lang.NullPointerException

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

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

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

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

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

E/AndroidRuntime(29187): ... 11 更多

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

各空 2024-09-11 14:22:22

我决定创建自己的活动,它只是在屏幕上绘制图像。这不是一个完美的解决方案,但它符合我的基本标准......它有效:)

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

人疚 2024-09-11 14:22:22

我用这个代码

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

i use 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);
                            }
                        });
゛清羽墨安 2024-09-11 14:22:21

我用这个 hack 来修复错误:

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

I used this hack to fix error:

...
Uri hacked_uri = Uri.parse("file://" + uri.getPath());
intent.setDataAndType(hacked_uri, "image/*");
...
飘过的浮云 2024-09-11 14:22:21

我在我的应用程序中使用了这段代码并且工作正常。我只做了一点改变。

我改变了这个:

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

为此:

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