简体   繁体   English

如何处理从不同设备的图库和相机中拾取的图像?

[英]How to handle picked images from gallery and camera in different devices?

I have these codes to get an image from gallery or take photo with camera and after cropping it i want to show it in a bitmap imageview . 我有这些代码可以从图库中获取图像或使用相机拍照,并在裁剪后想imageviewimageview显示它。

My problem is they work good in some devices and don't work in others! 我的问题是,它们在某些设备上运行良好,而在其他设备上却无法运行! please tell me which part of my code is making the problem ! 请告诉我代码的哪一部分导致了问题! Thank you 谢谢

For example: On LG G4 i not able to crop , but the photo from gallery and camera shows on image view On Samsung Galaxy Alpha i can crop , image from camera shows good but i cant choose from gallery! 例如:在LG G4上,我无法裁剪,但是图库和相机中的照片显示在图像视图上。在三星Galaxy Alpha上,我可以裁剪,相机中的图像显示良好,但是我不能从图库中选择!

What should i do to work fine in all devices? 我应该怎么做才能在所有设备上正常工作?

    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
    }
}

I think the problem must be in how i handle the returned data in onActivityResult method! 我认为问题一定出在我如何处理onActivityResult方法中返回的数据! Please take a look at my code: 请看一下我的代码:

@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);                
        }
    }
}

Possible solutions would be to put uri with imageChooser 's intent. 可能的解决方案是将uriimageChooser的意图放在一起。 Like this; 像这样;

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();
    }
}
  • On selecting from gallery, complete data is returned with intent. 从图库中选择时,将有意返回完整的数据。 You can use uri by accessing intent.getData() or thumbnail Bitmap using intent.getExtras().get("data") . 您可以通过使用intent.getExtras().get("data")访问intent.getData()或缩略图Bitmap来使用uri Don't use imageUri that you passed with that intent. 不要使用通过该意图传递的imageUri
  • On capturing from camera, in some devices intent is not null, but it's data is null. 从相机捕获时,在某些设备中,intent不为空,但其数据为空。 And in some devices, intent is null. 在某些设备中,intent为空。 Check both in onActivityResult . 同时检查onActivityResult

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

Use this uri to display/upload your image. 使用此uri显示/上传图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存从图库中挑选的图像? - How to save images which are picked from gallery? 如何在pdf报告中添加从相机或图库捕获的图像? - How to add captured images from camera or gallery in pdf report? 使用改造2将图像从图库/相机上传到服务器(okhttp问题) - Uploading images to server from gallery/camera using retrofit 2 (okhttp issue) 如何将相机拍摄的图像存储在图库中? - How to store image captured from camera in gallery? 从图库中选取的图像 ImageView 消失了 - Image Picked from Gallery ImageView Disappeard 在imageView中设置从图库中选取的图像 - Set an image picked from gallery in an imageView 缩放或按比例缩放从图库中选取的图像 - Scaling or proportionally resizing image picked from the gallery 如何在同一活动中分别从图库中选择两个不同的图像,并通过不同的点击将它们加载到它们的图像视图中? - How to pick two different images from gallery seperately in the same activity and load them into their imageviews at different clicks? 在 Android 中,如何检查从图库中选择的图像是否已被用户标记为收藏? - In Android, how to check if an image picked from gallery has been marked as favourite by the user? 如何仅从图库中获取图像 - How to get only images from gallery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM