Skip to content

Instantly share code, notes, and snippets.

@dafi
Last active December 28, 2015 16:09
Show Gist options
  • Save dafi/7527494 to your computer and use it in GitHub Desktop.
Save dafi/7527494 to your computer and use it in GitHub Desktop.
android - Working with MediaStore to share image from buffer and obtain the uri real path
public static String getRealPathFromImageURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
public static void shareImage(Context context, byte[] buffer, String mimeType, String subject, String chooserTitle) {
ContentValues values = new ContentValues(1);
values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream os = context.getContentResolver().openOutputStream(uri);
os.write(buffer);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType(mimeType);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri) ;
context.startActivity(Intent.createChooser(sharingIntent, chooserTitle));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment