首先讓我說這個問題與我的another question略有關聯。其實它是因爲這個而創建的。啓動Intent.ACTION_VIEW無意在保存的映像文件上工作
我有下面的代碼寫從網上下載到的文件在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,因爲在我的代碼中,我在不同的地方調用了這些方法,當我創建intent時,我有一個字符串路徑而不是文件。
現在我的問題。該文件被創建。意圖啓動一個對話框,要求我選擇一個查看器。我有3個觀衆安裝。天文圖像查看器,默認媒體庫(我2.1上有一個milstone,但在里程碑2.1更新沒有包括3D畫廊,所以它是舊的)和3D畫廊從nexus之一(我發現apk中的野生)。
現在,當我啓動3名觀衆以下發生:
天文圖像瀏覽器:活動 發佈會,但我看到的只是一個 黑屏。
媒體庫:我得到一個異常顯示「應用媒體 畫廊(過程 com.motorola.gallery)已停止 意外請再試一次。」與 力關閉選項 對話框。
3D gallery:一切正常,因爲它 應該。
當我嘗試簡單地使用天文文件管理器打開文件(瀏覽並只需點擊)我得到了相同的選項對話框,但是這一次情況有所不同:
天文圖片瀏覽器:一切正常 應該如此。
媒體庫:一切正常,因爲它 應該。
3D圖庫:活動推出,但 我看到的只是一個黑色的屏幕。
正如你所看到的一切都是一團糟。我不知道爲什麼會發生這種情況,但每次都會發生這種情況。這不是一個隨機錯誤。
我在創作意圖時錯過了什麼?或者當我創建圖像文件?有任何想法嗎?
編輯:正如在評論中指出的,這裏是adb logcat中感興趣的部分。另外我應該注意到,我改變了我創建圖像文件的方式。因爲我想創建一個反映在線文件的文件,我只需下載它,而不是創建一個位圖,然後創建該文件(這是因爲在某些時候我需要位圖,但現在我以相反的方式執行)。問題仍然存在,並且完全一樣:
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
com.motorola.gallery'崩潰的'adb logcat'輸出是什麼?另外,'image/*'在技術上不是一個有效的mimetype,你可能需要使用正確的'image/jpeg'。 – 2010-06-02 07:39:53
也發佈了日誌貓轉儲 – 2010-06-02 07:54:49
也不是,與SDK捆綁在一起的演示應用程序提供了一個示例,將意圖啓動爲「audio/*」?我認爲這將是相同的圖像... – 2010-06-02 07:55:41