2015-11-11 35 views
1

此代碼用於處理以前版本的Android,因爲來自onActivityResult的意圖的數據將返回null,如果圖像來自相機,並且它將保存如果圖像是從文件系統中選擇的,則爲Uri。然而,在棒棒糖上,相機意圖也在返回一個Uri。我如何知道圖像是用相機拍攝的還是從文件存儲/圖庫中選擇的?如何知道圖像是來自相機還是文件系統

這裏是我的代碼:

所有的
private void openImageIntent() { 

     // Determine Uri of camera image to save. 
     final File root = new File(Environment.getExternalStorageDirectory() 
       + File.separator + "Klea" + File.separator); 
     root.mkdirs(); 
     final String fname = getUniqueImageFilename(); 
     final File sdImageMainDirectory = new File(root, fname); 
     outputFileUri = Uri.fromFile(sdImageMainDirectory); 

     // Camera. 
     final List<Intent> cameraIntents = new ArrayList<Intent>(); 
     final Intent captureIntent = new Intent(
       android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     final PackageManager packageManager = getActivity().getPackageManager(); 
     final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
       captureIntent, 0); 
     for (ResolveInfo res : listCam) { 
      final String packageName = res.activityInfo.packageName; 
      final Intent intent = new Intent(captureIntent); 
      intent.setComponent(new ComponentName(res.activityInfo.packageName, 
        res.activityInfo.name)); 
      intent.setPackage(packageName); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      cameraIntents.add(intent); 
     } 

     // Filesystem. 
     final Intent galleryIntent = new Intent(); 
     galleryIntent.setType("image/*"); 
     galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 

     // Chooser of filesystem options. 
     final Intent chooserIntent = Intent.createChooser(galleryIntent, 
       getString(R.string.chose_source)); 

     // Add the camera options. 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, 
       cameraIntents.toArray(new Parcelable[] {})); 

     startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE); 
    } 

    private static String getUniqueImageFilename() { 
     // TODO Auto-generated method stub 
     String fileName = "img_" + System.currentTimeMillis() + ".jpg"; 
     return fileName; 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.d("reqCode", Integer.toString(requestCode)); 
     Log.d("resCode", Integer.toString(resultCode)); 
     Log.d("data", data.toString()); 
     getActivity(); 
     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == REQUEST_IMAGE_CAPTURE) { 
       final boolean isCamera; 
       if (data == null) { 
        isCamera = true; 
       } else { 
        final String action = data.getAction(); 
        if (action == null) { 
         isCamera = false; 
        } else { 
         isCamera = action 
           .equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        } 
       } 

       Uri selectedImageUri; 
       if (isCamera) { 
        File file = new File(outputFileUri.toString()); 
        Log.d("old file", file.toString()); 
        String temp = file.toString().replace("file:", ""); 
        Log.d("new file", temp); 
        selectedImageUri = Uri.parse(temp); 
       } else { 
        Uri selectedImage = data.getData(); 
        String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

        Cursor cursor = getActivity().getContentResolver().query(
          selectedImage, filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        selectedImageUri = Uri.parse(cursor.getString(columnIndex)); 
        cursor.close(); 
       } 
      } 
     } 
    } 
+0

你的代碼是基於[這個答案](http://stackoverflow.com/questions/4455558/allow-用戶選擇相機或圖片庫的圖像/ 12347567#12347567),不是嗎? –

+0

你的代碼決定不是基於Url,而是基於** data.getAction()**,所以我不知道爲什麼棒棒糖會使它與衆不同。 –

+0

由於數據用於在圖像來自相機時返回null。沒有理由從空對象獲取數據。因此,如果指定「intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);」,則有關相機的老問題不會返回圖像。 –

回答

3

首先,可以爲你難保。用戶可以選擇任何在其清單中聲明意圖的應用程序,並且應用程序可以決定從文件系統中選擇一張照片。

其次,看看新createChooser()方法,它有一個IntentSender參數返回最終用戶的選擇,爲您分析一下,看this example(不幸的是,這是22及以上)

第三,你提供outputFileUriMediaStore.EXTRA_OUTPUT - 這個文件將通過相機有望創造,而是由文件系統選擇器忽略;現在如果你只在清理之前調用啓動意圖,你可以很容易地檢查文件是否包含新的圖片。

我同意你的conclusion是選擇基於data.dat文件content://media/… - >文件,file:/… - >攝像頭)是不夠可靠。

所以底線,很容易,但強大的區分,可作出這樣的:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQUEST_IMAGE_CAPTURE) { 
      final boolean isCamera = new File(outputFileUri.getPath()).length() > 0; 
… 
+0

謝謝。好的解決方案,絕對比我的臨時好。 –

相關問題