2015-10-19 56 views
1

在我的應用程序按鈕點擊後,我想顯示在我的應用程序中選擇可用的照片應用程序。我們的目標是從其中一個與照片相關的應用中選擇圖片,並將其顯示在我的應用中。意圖打開保管箱在Android中選擇圖像

按鈕點擊下面的方法後,正在執行:

private void fireIntentToOpenDeviceImageResources() { 
    Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); 
    getIntent.setType("image/*"); 

    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    pickIntent.setType("image/*");   

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.Images.Media.TITLE, "New Picture"); 
    values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera"); 
    imageUri = getContext().getContentResolver().insert(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    takePicture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

    Intent chooserIntent = Intent.createChooser(getIntent, "Select Image"); 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent, takePicture}); 

    startActivityForResult(chooserIntent, GlobalConsts.PICK_IMAGE_REQUEST); 
} 

的問題是,我沒有看到Dropbox的應用程序(這是肯定的安裝在我的測試手機)中創建選擇器。我應該添加什麼樣的意圖,以包括Dropbox應用程序?

編輯:

我說這樣的意圖:

PackageManager manager = getActivity().getPackageManager(); 
Intent i = manager.getLaunchIntentForPackage("com.dropbox.android"); 
i.setAction(Intent.ACTION_GET_CONTENT);  
getIntent.setType("image/*"); 

現在它顯示在選配的Dropbox應用程序,但我不能從中挑選圖像。它只是啓動Dropbox應用程序而無需選擇並返回到我的應用程序的可能性。

+0

http://stackoverflow.com/questions/18870169/pick-image-from-gallery-camera-dropbox-etc – Pavan

回答

4

Android 4.4(API級別19)引入了Storage Access Framework (SAF),具有集中式文檔訪問權限。你可以用這個簡單的代碼打開的文檔選擇器:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); 
getIntent.setType("image/*"); 
startActivityForResult(getIntent, MY_REQUEST_CODE); 

爲此在Android> = 19你的代碼將打開一個包含各種應用目的選擇器,例如CameraPhotos(等等,等等...取決於安裝的應用程序)和Documents。如果您選擇Documents,文檔選擇器將打開並在側欄上顯示設備中安裝的所有不同應用程序,包括Dropbox。

如果您想獲得的Dropbox將在意向選擇器直接使用你可以改變你這樣的代碼:

private void fireIntentToOpenDeviceImageResources() { 
    Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    pickIntent.setType("image/*"); 

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.Images.Media.TITLE, "New Picture"); 
    values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera"); 
    Uri imageUri = MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    takePicture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

    Intent dropboxIntent = new Intent(Intent.ACTION_GET_CONTENT); 
    dropboxIntent.setPackage("com.dropbox.android"); 
    dropboxIntent.setType("image/*"); 

    Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image"); 
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePicture, dropboxIntent}); 

    startActivityForResult(chooserIntent, GlobalConsts.PICK_IMAGE_REQUEST); 
} 

警告!利用這種技術,如果你也想加入,你需要手動添加每一個應用程序,它只是在文件選擇器是可用的,例如ES中,你需要創建的意圖,並添加到EXTRA_INITIAL_INTENTS列表文件管理器:

Intent esFileManagerIntent = new Intent(Intent.ACTION_GET_CONTENT); 
esFileManagerIntent.setPackage("com.estrongs.android.pop"); 
esFileManagerIntent.setType("image/*"); 

Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image"); 
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePicture, dropboxIntent, esFileManagerIntent}); 
+0

偉大的信息。你的回答有助於解決我的問題。謝謝! – user2999943