4

我試圖讓用戶從圖庫或通過使用相機拍攝圖片來選擇圖像。我試過這個:在相機和圖庫之間選擇圖像選擇

 Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT); 
     imageIntent.setType("image/*"); 
     startActivityForResult(Intent.createChooser(imageIntent, "Select Picture"), GET_IMAGE_REQUEST); 

但它自動顯示畫廊,甚至沒有提供選擇活動的選項。似乎應該有一些更好的方法來完成這個比this question給出的解決方案。這真的是唯一的辦法嗎?

回答

5

你應該在你的應用程序中做這個邏輯。從畫廊中挑選圖像和使用相機拍攝照片的用途各不相同。

我建議你使用按鈕(或任何用戶界面,使用戶選擇一個動作),併爲兩個動作創建兩個單獨的方法。假設您創建了兩個按鈕btnPickGallerybtnTakePicture

兩個按鈕都會觸發自己的操作,如onBtnPickGalleryonBtnTakePicture

public void onBtnPickGallery() { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_REQUEST_CODE); 
} 

public void onBtnTakePicture() { 
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    File photo = new File(Environment.getExternalStorageDirectory(), "dir/pic.jpg"); 

    Uri outputFileUri = Uri.fromFile(photo); 

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE); 
} 

然後您可以使用onActivityResult()方法獲取結果。

8

我已經合併了一些解決方案,以便從Gallery或Camera中選取圖片的完整util。這些都是ImagePicker util(也以Github lib)功能:

  • 的畫廊和相機resquests合併意向。
  • 調整所選大的圖像(例如:2500×1600)
  • 旋轉圖像如果necesary

截圖:

ImagePicker starting intent

編輯:這裏是代碼的片段獲取圖庫和相機應用程序的合併意圖。 您可以在ImagePicker util看到完整的代碼(也處於Github lib

public static Intent getPickImageIntent(Context context) { 
    Intent chooserIntent = null; 

    List<Intent> intentList = new ArrayList<>(); 

    Intent pickIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    takePhotoIntent.putExtra("return-data", true); 
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context))); 
    intentList = addIntentsToList(context, intentList, pickIntent); 
    intentList = addIntentsToList(context, intentList, takePhotoIntent); 

    if (intentList.size() > 0) { 
     chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), 
       context.getString(R.string.pick_image_intent_text)); 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{})); 
    } 

    return chooserIntent; 
} 

private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) { 
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0); 
    for (ResolveInfo resolveInfo : resInfo) { 
     String packageName = resolveInfo.activityInfo.packageName; 
     Intent targetedIntent = new Intent(intent); 
     targetedIntent.setPackage(packageName); 
     list.add(targetedIntent); 
    } 
    return list; 
} 
+0

謝謝,這正是我一直在尋找(與OP過,我打賭)。我只是對代碼進行了一些修改(例如刪除內部的類包裝'rotateOrientationCall'和類似的東西),但它運行得非常好^^ – Ilario 2015-09-14 08:50:33

+0

感謝@llario隨意分發或評論要點以改進代碼。 – 2015-09-14 09:21:27

+1

我改進了圖庫圖像的旋轉和調整大小的方法。一探究竟。 – 2015-10-02 09:38:39