dimanche 10 juillet 2016

How to receive and save image shared from another application

if(intent.getType().startsWith("image/")){
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    imageView.setImageUri(imageUri);
}

I need to save this image in my applications data directory. Queering on content resolver returns path to only those image files which are already saved in the device. like,

private String getPathFromURI(Uri contentURI) {
        String result = null;
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(contentURI, null, null, null, null);
            cursor.moveToFirst();
            int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return result;
    }

but this does not works all the times because some of those image files are in private directories of the apps. What to do to get read access to such files, so that I can write copies of them for my application? is there something wrong, that I should do some other way? kindly help, thank you.

Aucun commentaire:

Enregistrer un commentaire