2016-09-20 70 views
0

閱讀我的整個代碼。除了MarshmallowLollipop之外,它在每部手機上都能正常工作。在MarshmallowLollipop電話唯一的問題是data.getData()返回null只在圖片捕獲。如果我捕捉視頻。我這是工作的罰款有4個案例 -onActivityResult返回意圖data.getData();總是空在棉花糖和棒棒糖

  1. 從庫選擇圖片,並顯示其在Imageview縮略圖 - 做工精細。

  2. 捕捉圖片的了攝像機,顯示Imageview其縮略圖 - MarshmallowLollipop不工作

  3. 從庫中選擇視頻並在Imageview中顯示其縮略圖 - 正常工作。

  4. 3.從相機捕捉視頻並在Imageview中顯示其縮略圖 - 正常工作。

現在有很多解決方案,但都沒有看上去令人滿意,如果代碼是在視頻捕捉的情況下工作正常,那麼爲什麼它不用於圖像拍攝的工作?它是一個錯誤還是我做錯了什麼地方?

這是我的代碼。我曾經評論過的行,其中碰撞happens-

@Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     int THUMBSIZE = 120; 
     switch (requestCode){ 


     case RESULT_LOAD_IMAGE: 


      if (resultCode==RESULT_OK){ 
       String column_Name= MediaStore.Images.Media.DATA; 
       String picturePath=getPath(column_Name,data.getData()); 
       Bitmap orientedBitmap = ExifUtil.rotateBitmap(picturePath, BitmapFactory.decodeFile(picturePath)); 
       Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap, THUMBSIZE, THUMBSIZE)); 
       thumbnailview.setBackground(fullpic); 
       editText.setText(picturePath); 
       picker.setVisibility(View.VISIBLE); 
       thumbnailview.setClickable(false); 

      } 
      break; 

     case REQUEST_IMAGE_CAPTURE: 

      if (resultCode==RESULT_OK){ 
       String picturePath=""; 
       String column_Name=MediaStore.Images.Media.DATA; 

       picturePath=getPath(column_Name,data.getData()); 

        //My app Crashes here because in Marshmallow data.getData() is always null. 

       Bitmap orientedBitmap = ExifUtil.rotateBitmap(picturePath, BitmapFactory.decodeFile(picturePath)); 
       Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap, THUMBSIZE, THUMBSIZE)); 
       thumbnailview.setBackground(fullpic); 
       editText.setText(picturePath); 
       picker.setVisibility(View.VISIBLE); 
       thumbnailview.setClickable(false); 


      } 
      break; 

     case RESULT_LOAD_VIDEO: 

      if (resultCode==RESULT_OK){ 
       String column_Name=MediaStore.Video.Media.DATA; 
       String videoPath=getPath(column_Name,data.getData()); 
       Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND)); 
       thumbnailview.setBackground(fullpic); 
       editText.setText(videoPath); 
       picker.setVisibility(View.VISIBLE); 
       thumbnailview.setClickable(false); 


      } 
      break; 

     case REQUEST_VIDEO_CAPTURE: 

      if (resultCode==RESULT_OK){ 
       String column_Name=MediaStore.Video.Media.DATA; 
       String videoPath=getPath(column_Name,data.getData()); 
       Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND)); 
       thumbnailview.setBackground(fullpic); 
       editText.setText(videoPath); 
       picker.setVisibility(View.VISIBLE); 
       thumbnailview.setClickable(false); 


      } 
      break; 
    } 

    if (picker != null) { 
     picker.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       OpenDialog(); 
      } 
     }); 
    } 

} 


private String getPath(String column_Name,Uri uri){ 

    String[] projection = {column_Name}; 
    String path = ""; 
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
    int column_index_data ; 
    if (cursor != null) { 
     column_index_data = cursor.getColumnIndexOrThrow(column_Name); 
     cursor.moveToFirst(); 
     path = cursor.getString(column_index_data); 
     cursor.close(); 
    } 

    return path; 
} 

private void OpenDialog(){ 
    dialogBox.setTitle("Select an Action"); 
    dialogBox.setMessage("Choose Picture or Video"); 

    dialogBox.setPositiveButton("Picture", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      if (check_Permissions()){ 
       OpenCameraDialog(); 
      } 
      else { 
       request_Permissions(); 
       CAMERA_DIALOG_PERMISSION=1; 

      } 
     } 
    }); 

    dialogBox.setNegativeButton("Video", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      if (check_Permissions()){ 
       OpenVideoDialog(); 
      } 
      else { 
       request_Permissions(); 
       VIDEO_DIALOG_PERMISSION=1; 
      } 

     } 
    }); 

    dialogBox.show(); 

} 



private void OpenCameraDialog(){ 

    dialogBox.setTitle("Select an Action"); 
    dialogBox.setMessage("Choose Picture From"); 

    dialogBox.setPositiveButton("Gallery", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      final Intent galleryintent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(galleryintent, RESULT_LOAD_IMAGE); 
     } 
    }); 

    dialogBox.setNegativeButton("Camera", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 

     } 
    }); 

    dialogBox.show(); 

} 

private void OpenVideoDialog(){ 

    dialogBox.setTitle("Select an Action"); 
    dialogBox.setMessage("Choose Video From"); 

    dialogBox.setPositiveButton("Gallery", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      final Intent galleryintent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(galleryintent, RESULT_LOAD_VIDEO); 
     } 
    }); 

    dialogBox.setNegativeButton("Camera", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      final Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 

      if (takeVideoIntent.resolveActivity(getPackageManager()) != null) { 
       startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE); 
      } 
     } 
    }); 
    dialogBox.show(); 

} 

private boolean check_Permissions(){ 

    boolean GRANTED; 

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) + 
      ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) + 
      ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) + 
      ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){ 
     GRANTED=false; 

    } 
    else { 
     GRANTED=true; 
    } 
    return GRANTED; 
} 

private void request_Permissions(){ 
    ActivityCompat.requestPermissions(this, 
      new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO}, 
      REQUEST_FOR_PERMISSION); 

} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode){ 

     case REQUEST_FOR_PERMISSION: 

      if ((grantResults.length>0)&& (grantResults[0] +grantResults[1]+grantResults[2]+grantResults[3]== PackageManager.PERMISSION_GRANTED)){ 

       if (CAMERA_DIALOG_PERMISSION==1){ 
        OpenCameraDialog(); 
        CAMERA_DIALOG_PERMISSION=0; 
       } 
       else if (VIDEO_DIALOG_PERMISSION==1){ 
        OpenVideoDialog(); 
        VIDEO_DIALOG_PERMISSION=0; 
       } 

      } 
      else { 
       Toast.makeText(this, "Please GRANT Permissions", Toast.LENGTH_SHORT).show(); 
      } 

    } 

} 

回答

0

以上,通過@CommonsWare建議 -

相機應用不需要返回URI。

此外,

你需要告訴相機的應用程序在哪裏寫的圖像。

所以,我代替我的代碼與 -

final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      pictureUri=getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri); 
      startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 

onActivityResult -

case REQUEST_IMAGE_CAPTURE: 

      if (resultCode==RESULT_OK){ 
       String picturePath=""; 
       String column_Name= MediaStore.Images.Media.DATA; 
       if (data!=null){ 
       if (data.getData()!=null){ 
       picturePath=getPathfromUri(column_Name,data.getData());} 

       else { 
        picturePath= pictureUri.getPath(); 

       } 
       } 
       else { 
        picturePath= pictureUri.getPath(); 

       }} 
1

這是每個手機可以正常使用,除了棉花糖和棒棒糖

不,事實並非如此。它會在很多Android版本上失敗,當用戶選擇一個寫得很好的相機應用程序來處理您的ACTION_IMAGE_CAPTURE請求。

你的問題就在這裏:

  if (data.getData()!=null){ 
      picturePath=getPath(column_Name,data.getData());} 
      else { 
       //My app Crashes here because in Marshmallow data.getData() is always null. 
      } 

這裏至少有兩個缺陷。

最大的假設是你從ACTION_IMAGE_CAPTURE得到UriThat is not documented,相機應用程序不需要返回Uri。特別是,在您的結構中,您只能通過getExtra("data")獲得縮略圖。如果您需要全尺寸圖像,請在您的Intent上使用EXTRA_OUTPUT,在這種情況下,您知道圖像的存儲位置。—無論您在何處指示EXTRA_OUTPUT

另一個是,你認爲你回來的Uri是從MediaStore或其他有MediaStore.Video.Media.DATA列。相機應用程序不僅不必返回Uri,但不要求Uri來自MediaStore或具有這樣的列。

+0

現在看,我,我不使用的if else更新了我的代碼..我貼錯了對不起 –

+0

我應該通過putExtra和EXTRA OUTPUT一起傳遞什麼信息? @CommonsWare –

+0

@KarandeepAtwal:理想情況下,使用由「ContentProvider」支持的'Uri'。你可以使用'file:''Uri',但是在Android 7.0+上使用'targetSdkVersion'爲24+不起作用。有關示例,請參見[此示例項目](https://github.com/commonsguy/cw-omnibus/tree/master/Camera/FileProvider)。 – CommonsWare