私はギャラリーから画像を取得するか、カメラで写真を撮るためのコードを持っており、それを切り取った後、ビットマップ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);
}
}
}