简体   繁体   English

使用谷歌照片的裁剪意图不起作用

[英]Crop Intent using google photos not working

I am trying to make an app to select an image from the gallery or google photos and then crop it to make it my app background.我正在尝试制作一个应用程序来从图库或谷歌照片中选择一张图片,然后裁剪它以使其成为我的应用程序背景。 The problem I am facing is that When I try to crop the picture using google photos then it gets saved but the app background doesn't change and I don't recive any crashes or any errors but when I crop it using the gallery app everything seems to work perfectly.我面临的问题是,当我尝试使用谷歌照片裁剪图片时,它会被保存,但应用程序背景没有改变,我没有收到任何崩溃或任何错误,但是当我使用图库应用程序裁剪它时,一切正常似乎完美地工作。

Here is my code to select photos:这是我选择照片的代码:

Intent i = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(i, SELECT_PICTURE);

case SELECT_PICTURE: {
            if (resultCode == RESULT_OK && null != data) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {

                        Uri selectedImageUri = data.getData();
                        InputStream imageStream;
                        Bitmap selectedImage;
                        try {
                            cropCapturedImage(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri));
                        } catch (ActivityNotFoundException aNFE) {
                            //display an error message if user device doesn't support
                            showToast(getString(R.string.error_crop_not_supported));
                            try {
                                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                                Cursor cursor = getContentResolver().query(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri), filePathColumn, null, null, null);
                                cursor.moveToFirst();
                                imageStream = getContentResolver().openInputStream(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri));
                                selectedImage = BitmapFactory.decodeStream(imageStream);

                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                                byte[] b = baos.toByteArray();
                                String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                                //SharePreference to store image
                                PrefManager.putString(Constant.IMAGE_DATA, encodedImage);
                                cursor.close();
                                //set gallery image
                                setChatBackground();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }

Here is my cropIntent code:这是我的cropIntent代码:

public void cropCapturedImage(Uri picUri) {
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    cropIntent.setDataAndType(picUri, "image/*");
    cropIntent.putExtra("crop", "true");
    cropIntent.putExtra("aspectX", 9);
    cropIntent.putExtra("aspectY", 14);
    cropIntent.putExtra("outputX", 256);
    cropIntent.putExtra("outputY", 256);
    cropIntent.putExtra("return-data", true);
    startActivityForResult(cropIntent, CROP_PICTURE);
}

ase CROP_PICTURE: {
            Uri uri = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
            if(cursor.moveToFirst()){
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String yourRealPath = cursor.getString(columnIndex);
            } else {
                //boooo, cursor doesn't have rows ...
            }
            cursor.close();
            String v= data.toString();

            if (resultCode == RESULT_OK && null != data) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Bundle extras = data.getExtras();
                            Bitmap thePic = extras.getParcelable("data");
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            thePic.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                            byte[] b = baos.toByteArray();
                            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                            //SharePreference to store image
                            PrefManager.putString(Constant.IMAGE_DATA, encodedImage);
                            //set gallery image
                            setChatBackground();
                        } catch (NullPointerException e) {
                            Log.d(TAG, e.getLocalizedMessage());
                        }
                    }
                });
            }

No, Android Does Not Have a Crop Intent不, Android 没有裁剪意图

Calling method startActivity() on an Intent with an action of com.android.camera.action.CROP .使用com.android.camera.action.CROP操作在 Intent 上调用方法startActivity() They are doing this to crop an image.他们这样做是为了裁剪图像。

This is a really bad idea.这真是一个糟糕的主意。

Source here - CommonsBlog 来源在这里 - CommonsBlog

在此处输入图片说明

Try to log your selectedImageUri first.首先尝试记录您selectedImageUri ImageUri。

I suspect that the google photos returns a url to online file, while the gallery app return a local file path.我怀疑谷歌照片会返回一个指向在线文件的网址,而图库应用会返回一个本地文件路径。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM