Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick Image From Gallery Or Google Photos Failing

EDIT: I wanted to update what the problem really is. On a Samsung phone you have "Gallery", inside "Gallery" you can also see your google photos backups. This is the photo selection I'm trying to get at. Now on other apps (Close5) they simply display a message stating "That photo is not supported". But why? Why can't we get this url (https:/lh3.googleusercontent) to work?

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /https:/lh3.googleusercontent.com/-Llgk8Mqk-rI/U8LK0SMnpWI/AAAAAAAAFk4/0cktb59DnL8GgblJ-IJIUBDuP9MQXCBPACHM/I/IMG_20140711_085012.jpg: open failed: ENOENT (No such file or directory)

You can all clearly see this is a real photo with a real url:

https:/lh3.googleusercontent.com/-Llgk8Mqk-rI/U8LK0SMnpWI/AAAAAAAAFk4/0cktb59DnL8GgblJ-IJIUBDuP9MQXCBPACHM/I/IMG_20140711_085012.jpg

Everything works fine until I go to select from photos or a google photos image instead of from the gallery, I tried handling the exception (Sorry, that image isn't supported. Try another gallery!) but that seems incomplete. I tried looking at other solutions on here but none of them worked for google photos. Thanks!

Error is a null pointer with:

app E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /https:/lh3.googleusercontent

 @Override
    public void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
                if (requestCode == 1) {
                    try{
                    String selectedImagePath = getAbsolutePath(data.getData());                          ((PostAdParentTabHost)getActivity()).getMap_of_picture_paths().put(1, selectedImagePath);                      
                    post_ad_camera_img_1.setImageBitmap(decodeSampledBitmapFromFileToCustomSize(selectedImagePath, 150, 150));
                    animation_master.fade(post_ad_camera_img_1);
                    }catch(Exception e){
                        e.printStackTrace();
                        ((PostAdParentTabHost)getActivity()).getMap_of_picture_paths().remove(requestCode);
                        Toast.makeText(getActivity(), "Sorry, that image isn't supported. Try another gallery! ", Toast.LENGTH_SHORT).show();
                    }

//------------------------------------------------------Methods:
public String getAbsolutePath(Uri uri) {
    Uri final_uri = uri;
        if (uri.getLastPathSegment().contains("googleusercontent.com")){
            final_uri = getImageUrlWithAuthority(getActivity(), uri);
            String[] projection = { MediaStore.MediaColumns.DATA };
            Cursor cursor = getActivity().getContentResolver().query(final_uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }else{

                return null;
            }
        }else{
        String[] projection = { MediaStore.MediaColumns.DATA };
        Cursor cursor = getActivity().getContentResolver().query(final_uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }else{

            return null;
        }

    }

 public static Uri getImageUrlWithAuthority(Context context, Uri uri) {
    InputStream is = null;
    if (uri.getAuthority() != null) {
        try {
            is = context.getContentResolver().openInputStream(uri);
            Bitmap bmp = BitmapFactory.decodeStream(is);
            return writeToTempImageAndGetPathUri(context, bmp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

      public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory()
                + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.image_as_uri_for_parcel = imgUri;
        this.imgPath = file.getAbsolutePath();
        return imgUri;

}
like image 515
Petro Avatar asked Dec 11 '22 18:12

Petro


1 Answers

Try getPathFromInputStreamUri(), this will fix the content uri problem from google photo app

Kotlin (UPDATED)

fun getPathFromInputStreamUri(context: Context, uri: Uri): String? {
    var filePath: String? = null
    uri.authority?.let {
        try {
            context.contentResolver.openInputStream(uri).use {
                val photoFile: File? = createTemporalFileFrom(it)
                filePath = photoFile?.path
            }
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
    return filePath
}

@Throws(IOException::class)
private fun createTemporalFileFrom(inputStream: InputStream?): File? {
    var targetFile: File? = null
    return if (inputStream == null) targetFile
    else {
        var read: Int
        val buffer = ByteArray(8 * 1024)
        targetFile = createTemporalFile()
        FileOutputStream(targetFile).use { out ->
            while (inputStream.read(buffer).also { read = it } != -1) {
                out.write(buffer, 0, read)
            }
            out.flush()
        }
        targetFile
    }
}

private fun createTemporalFile(): File = File(getDefaultDirectory(), "tempPicture.jpg")

Java

public static String getPathFromInputStreamUri(Context context, Uri uri) {
    InputStream inputStream = null;
    String filePath = null;

    if (uri.getAuthority() != null) {
        try {
            inputStream = context.getContentResolver().openInputStream(uri);
            File photoFile = createTemporalFileFrom(inputStream);

            filePath = photoFile.getPath();

        } catch (FileNotFoundException e) {
            Logger.printStackTrace(e);
        } catch (IOException e) {
            Logger.printStackTrace(e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return filePath;
}

private static File createTemporalFileFrom(InputStream inputStream) throws IOException {
    File targetFile = null;

    if (inputStream != null) {
        int read;
        byte[] buffer = new byte[8 * 1024];

        targetFile = createTemporalFile();
        OutputStream outputStream = new FileOutputStream(targetFile);

        while ((read = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, read);
        }
        outputStream.flush();

        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return targetFile;
}

private static File createTemporalFile() {
    return new File(Utils.getDefaultDirectory(), "tempPicture.jpg");
}
like image 76
Arindam Karmakar Avatar answered Dec 30 '22 11:12

Arindam Karmakar