如何在不同的设备中处理从画廊和相机中挑选的图像?

时间:2016-09-09 09:50:19

标签: java android android-studio

我有这些代码可以从图库中获取图片或使用相机拍照,裁剪后我想在位图imageview中显示。

我的问题是它们在某些设备上运行良好,而在其他设备上无法工作! 请告诉我我的代码的哪一部分正在解决问题!谢谢

例如: 在LG G4我无法裁剪,但图库视图中的画廊和相机显示的照片 在三星Galaxy 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);                
        }
    }
}

1 个答案:

答案 0 :(得分:0)

可能的解决方案是将uri置于imageChooser的意图之下。像这样;

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()或缩略图Bitmapintent.getExtras().get("data")使用imageUri。不要使用您带有该意图的onActivityResult
  • 从相机捕获时,在某些设备中,intent不为null,但它的数据为null。在某些设备中,intent为null。检查Uri uri; if (intent == null || intent.getData() == null) { uri = this.imageUri; } else { uri = intent.getData(); }

    uri

使用此 $("#screenname").empty(); $("#screenname").append('<option value=0>Select Screen</option>'); $.each(data.screens,function(index,screenobj){ $("#screenname").append('<option value="' +screenobj.screen_id + '">' +screenobj.screen_name +'</option>'); }); 显示/上传您的图片。