2016-09-09 7 views
0

私はギャラリーから画像を取得するか、カメラで写真を撮るためのコードを持っており、それを切り取った後、ビットマップimageviewで表示したいと思います。さまざまなデバイスのギャラリーとカメラからの選択されたイメージを扱うにはどうすればいいですか?

私の問題は、いくつかのデバイスでうまく動作し、他のデバイスではうまくいきません! 私のコードのどの部分が問題になっているか教えてください!トリミングするにはLGのG4で iはできませんが、カメラからの画像は良い示し、ギャラリーやカメラからの写真は、サムスンギャラクシーAlphaではイメージ図 に示して、私は切り抜くことができますが、私はギャラリーから選択カント:たとえば

ありがとう!

すべてのデバイスで正常に動作させるにはどうすればよいですか?

private void showFileChooser() { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    // ******** code for crop image 
    intent.putExtra("crop", "true"); 
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    intent.putExtra("outputX", 200); 
    intent.putExtra("outputY", 200); 
    intent.putExtra("cropped-rect", "true"); 

    try { 
     intent.putExtra("return-data", true); 
     startActivityForResult(Intent.createChooser(intent, 
       "Complete action using"), PICK_FROM_GALLERY); 
    } catch (ActivityNotFoundException e) { 
    // Do nothing for now 
    } 

    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
} 



    private void captureWithCamera() { 

    // call android default camera 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
    // ******** code for crop image 
    intent.putExtra("crop", "true"); 
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    intent.putExtra("outputX", 200); 
    intent.putExtra("outputY", 200); 
    intent.putExtra("cropped-rect", "true"); 

    try { 

     intent.putExtra("return-data", true); 
     startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_CAMERA); 
    } catch (ActivityNotFoundException e) { 
// Do nothing for now 
    } 
} 

問題は、返されたデータをどのようにonActivityResultメソッドで処理する必要があると思いますか。私のコードを見てみてください:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK && data != null && data.getData() != null) { 
     Uri filePath = data.getData(); 
     try { 
      //Getting the Bitmap from Gallery 
      bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); 
      //Setting the Bitmap to ImageView 
      imageView.setImageBitmap(bitmap); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    if (requestCode == PICK_FROM_CAMERA) { 
     Bundle extras = data.getExtras(); 
     if (extras != null) { 
      //Bitmap photo = extras.getParcelable("data"); 
      bitmap = extras.getParcelable("data"); 
      imageView.setImageBitmap(bitmap);     
     } 
    } 
} 

答えて

0

可能な解決策は、imageChooserの意図を持ってuriを置くことであろう。このような;ギャラリーから選択するには

private void showCamera() { 

    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "DCIM"); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 

     File localFile2 = new File(file + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 
     imageUri = Uri.fromFile(localFile2); 

     Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE"); 

     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      cameraIntent.setClipData(ClipData.newRawUri(null, Uri.fromFile(localFile2))); 
     } 

     startActivityForResult(cameraIntent, REQUEST_CAMERA); 
    } catch (Exception localException) { 
     Toast.makeText(ActivityAddMemory.this, "Exception:" + localException, Toast.LENGTH_SHORT).show(); 
    } 
} 
  • 、完全なデータが意図して返されます。 uriは、intent.getData()にアクセスするか、Bitmapのサムネイルintent.getExtras().get("data")を使用してアクセスできます。その意図に合格したimageUriは使用しないでください。
  • カメラからのキャプチャでは、一部のデバイスでは意図はnullではありませんが、データはnullです。一部のデバイスでは、インテントがnullです。両方をonActivityResultにチェックしてください。

    Uri uri; 
    if (intent == null || intent.getData() == null) { 
        uri = this.imageUri; 
    } else { 
        uri = intent.getData(); 
    } 
    

あなたのイメージをアップロード/表示するには、このuriを使用します。

関連する問題