Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Image from google photos library after dowloading

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.

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?

Here is my code:

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.

like image 799
Mostafa Addam Avatar asked Jan 04 '16 20:01

Mostafa Addam


People also ask

Can you recover photos from Google Photos?

You can restore deleted items from your Google Photos trash within 60 days after you deleted them. info Features are subject to availability.


1 Answers

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

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

             }
         }

}

Here is getPath() method which is used in 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 "";
}

I hope it helps you.

like image 114
Rajesh Avatar answered Sep 19 '22 12:09

Rajesh