2013-08-24 44 views
7

我試圖打開內置圖庫應用程序在我的應用程序中按下按鈕。在Android中打開圖庫應用程序

我嘗試了Android 2.3及以上版本的手機。在手機/平板電腦,我有是

三星S(的Android 2.3.5) LG手機(安卓2.3.3) 歌Nexus One(Android的2.3.6) Android平板電腦(安卓4.0.3) 的Galaxy Nexus (安卓4.3)

我試過如下:

Intent intent = new Intent(Intent.ACTION_VIEW, null); 
intent.setType("image/*"); 
startActivity(intent); 

上面的代碼工作在Android平板電腦(4.0.3)和我的Nexus手機也沒關係..但如果運行任何手機上相同的應用程序是低於3.0(給我錯誤)

08-24 11:47:53.628: E/AndroidRuntime(787): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.os.Looper.loop(Looper.java:130) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.app.ActivityThread.main(ActivityThread.java:3687) 
08-24 11:47:53.628: E/AndroidRuntime(787): at java.lang.reflect.Method.invokeNative(Native Method) 
08-24 11:47:53.628: E/AndroidRuntime(787): at java.lang.reflect.Method.invoke(Method.java:507) 
08-24 11:47:53.628: E/AndroidRuntime(787): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
08-24 11:47:53.628: E/AndroidRuntime(787): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
08-24 11:47:53.628: E/AndroidRuntime(787): at dalvik.system.NativeStart.main(Native Method) 
08-24 11:47:53.628: E/AndroidRuntime(787): Caused by: java.lang.NullPointerException 
08-24 11:47:53.628: E/AndroidRuntime(787): at com.cooliris.media.Gallery.onCreate(Gallery.java:323) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-24 11:47:53.628: E/AndroidRuntime(787): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
08-24 11:47:53.628: E/AndroidRuntime(787): ... 11 more 

所以我試過如下:

Intent intent1= new Intent("android.intent.action.MAIN", null); 
intent1.addCategory("android.intent.category.APP_GALLERY"); 
Intent intent2 = Intent.createChooser(intent1, "Gallery"); 
startActivity(intent2); 

同樣,這只是正常與高於/ equalto 4.0版本的手機。在4.0以下的手機,它通過說:

"No application can perform this action" 

有人可以幫我打開圖庫從我的應用程序按下按鈕?

+0

也許這樣。 http://stackoverflow.com/questions/6016000/how-to-open-phones-gallery-through-code –

回答

24

我想出的辦法..

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media")); 
startActivity(intent); 

這段代碼剛纔打開的畫廊,沒有任何問題。可以讓它適用於所有版本!

想到把它作爲正在尋找在所有版本上打開圖庫的人的答案。

謝謝! :)

+0

嗨,視頻庫怎麼樣? –

+1

謝謝,它爲我工作,但我不得不添加intent.setType(「圖像/ *」); – iFrey

+0

這將在Android 4.2設備上打開包含圖像和視頻相冊的圖庫。完善!謝謝。 – Hong

-2
Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(
        Intent.createChooser(intent, "Complete action using"), 
        PICK_FROM_FILE); 
+1

請記錄代碼的作用和原因。只是代碼的答案將最終轉到審查隊列進行手動檢查。 – hexafraction

3

嘗試了這一點

btnGallery.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE); 
    } 
}); 

UPDATE onActivityResult

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != Activity.RESULT_CANCELED) { 
     if (requestCode == PICK_IMAGE) { 
      Uri selectedImageUri = data.getData(); 
     } 
    } 
} 

UPDATE打開媒體庫APP

Intent galleryIntent = new Intent(Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivity(galleryIntent); 
+0

我試圖打開一個圖庫應用程序..上面的代碼恢復到我的應用程序.. – TheDevMan

相關問題