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

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

问题描述

首先让我说,这个问题稍微连接到<一个href="http://stackoverflow.com/questions/2951616/launch-intent-viewer-to-display-image-from-url">another问题由我的。实际上,它的形成是因为这一点。

我有以下的code写的净SD卡下载到文件的位图:

  //从URL图像
网址U =新的网址(URL);
HTTPGET HTT prequest =新HTTPGET(u.toURI());
HttpClient的HttpClient的=新DefaultHttpClient();
HTT presponse响应=(HTT presponse)httpclient.execute(HTT prequest);
HttpEntity实体= response.getEntity();
BufferedHttpEntity bufHttpEntity =新BufferedHttpEntity(实体);
InputStream的河道= bufHttpEntity.getContent();
位图bmImg = BitmapFactory.de codeStream(河道);
instream.close();

//将图像中的SD卡上的文件
文件海报文件=新File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/data/com.myapp/files/image.jpg");
posterFile.createNewFile();
的BufferedOutputStream OUT =新的BufferedOutputStream(新的FileOutputStream(海报文件));
位图可变= Bitmap.createScaledBitmap(bmImg,bmImg.getWidth(),bmImg.getHeight(),TRUE);
mutable.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
了out.flush();
out.close();

//启动默认浏览器的文件
意向意图=新的意图();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(posterFile.getAbsolutePath()),图像/ *);
((活动)的getContext())startActivity(意向)。
 

这几个音符。我看到有人使用后创造了可变位图,它似乎比工作,没有它好。而我使用的Uri类的解析方法,而不是因为在我的codeI的FROMFILE我在不同的地方调用这些,当我创建的意图我有一个字符串路径,而不是一个文件。

现在我的问题。该文件被创建。这样做的目的将启动一个对话框,要求我选择一个浏览器。我安装了3观众。天文图像浏览器,默认的媒体库(我有一个milstone的2.1,但在里程碑2.1更新不包括3D图库因此它是旧的)和Nexus One的三维画廊(我找到了APK中野)。

现在,当我启动3观众以下发生:

  • 天文图像查看器:活动 启动但我什么也看不见,但一 黑屏。

  • 媒体库:我得到一个异常 所示对话框中的应用媒体 画廊(过程 com.motorola.gallery)已经停止 不料。请重试与 一个强制关闭的选项。

  • 3D图库:一切正常,因为它 应该。

当我试图简单地使用天文文件管理器打开文件(浏览它,只需按一下),我得到了相同的选项对话框但这次的事情是不同的:

  • 天文图像查看器:拍卖作品 因为它应该。

  • 媒体库:一切正常,因为它 应该。

  • 3D图库:活动推出,但 我什么也看不见,但一个黑色的屏幕。

正如你所看到的一切都是一塌糊涂。我不知道为什么会这样,但是它发生这样的每一次。这不是一个随机的错误。

我缺少的东西,当我创建的意图是什么?或者,当我创建的映像文件?任何想法?

编辑:作为在这里评论指出,是在亚行logcat感兴趣的部分。同时我要指出,我改变了我创建映像文件的方式。因为我想创建一个反映一个在线文件我只是下载创建位图,然后创建该文件的它,而不是一个文件(这是在某些时候做的,因为我需要的位图,但现在我做的其他方式)。问题依然存在思想是完全一样的:

  

I / ActivityManager(18852):启动   活动:意向{   ACT = android.intent.action.VIEW   DAT = / SD卡/安卓/数据/ com.myapp /文件/ image.jpg的   (典型值)=图像/ * FLG = 0x3800000   CMP = com.motorola.gallery / .ViewImage}

     

I / ActivityManager(18852):启动PROC   com.motorola.gallery:ViewImage为   活动   com.motorola.gallery/.ViewImage:   PID = 29187的uid = 10017导报= {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):关闭   VM

     

W / dalvikvm(29187):主题ID = 3:螺纹   与未捕获的异常退出   (组= 0x4001b170)

     

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

     

E / AndroidRuntime(29187):   java.lang.RuntimeException的:无法   启动活动   ComponentInfo {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多个

解决方案

我用这个技巧来修复错误:

  ...
乌里hacked_uri = Uri.parse(文件://+ uri.getPath());
intent.setDataAndType(hacked_uri,图像/ *);
...
 

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

解决方案

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天全站免登陆