下载后从谷歌照片库中检索图像 [英] Retrieve Image from google photos library after dowloading

查看:86
本文介绍了下载后从谷歌照片库中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了从画廊获取照片的意图,当我在我的画廊中使用nexus google照片应用程序时,一切正常。

I am launching an intent to get photos from gallery and when I am using nexus google photo app in my gallery everything works fine.

但如果图像不是在手机上(在Google照片在线服务上)它将为我下载。选择图像后,我将图像发送到另一个活动进行裁剪,但在下载的情况下,发送到裁剪活动的图像为空,因为下载尚未完成。

But if the image is not on the phone (on the Google Photos online service) it will download it for me. After selecting the image I am sending the image to another activity for cropping but in case of download the image sent to the crop activity is null since the download is not finished yet.

我如何知道下载完成后将图像发送到裁剪活动?

How can I know when the download is finished to send the image to the cropping activity?

这是我的代码:

private void pickFromGallery()
{
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == Activity.RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            // Get the cursor
            Cursor cursor = getApplicationContext().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            assert cursor != null;
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            startCrop(imgDecodableString);
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }
    }

任何帮助都将不胜感激。

any help would be appreciated.

推荐答案

我认为从谷歌照片下载所选图像时无法裁剪图像。你只能裁剪你的本地存储图像

I think you can't crop images when you download selected image from google photos. you can only crop your local storage images

但是为了检查所选图像是否可下载或从本地存储中你可以在你的 onActivityResult中做到这一点( )方法。

But for checking whether selected image is downloadable or from local storage you can do like this in your onActivityResult() method.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMG && resultCode == Activity.RESULT_OK
                && null != data) {

                Uri selectedImageUri = data.getData();
                String tempPath = getPath(selectedImageUri, getActivity());
                String url = data.getData().toString();
                if (url.startsWith("content://com.google.android.apps.photos.content")){
                    try {
                        InputStream is = getActivity().getContentResolver().openInputStream(selectedImageUri);
                        if (is != null) {
                            Bitmap pictureBitmap = BitmapFactory.decodeStream(is);
                            //You can use this bitmap according to your purpose or Set bitmap to imageview
                        }
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }else {
                     startCrop(tempPath);

             }
         }

}

这是 getPath()方法,用于 onActivityResult()

public String getPath(Uri uri, Activity activity) {
    Cursor cursor = null;
    try {
        String[] projection = {MediaStore.MediaColumns.DATA};
        cursor = activity.getContentResolver().query(uri, projection, null, null, null);
        if (cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            return cursor.getString(column_index);
        }
    } catch (Exception e) {
    } finally {
        cursor.close();
    }
    return "";
}

我希望它可以帮到你。

这篇关于下载后从谷歌照片库中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆