Skip to content

Instantly share code, notes, and snippets.

@ParryPatel021
Created December 16, 2019 11:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ParryPatel021/5b368bfc6a72d5dff23ed798a13c8456 to your computer and use it in GitHub Desktop.
/**
* Get Image Uri from Image Bitmap
* @param inContext
* @param inImage
* @return Uri
*/
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "NewsImage", null);
return Uri.parse(path);
}
/**
* Get Video Path from Image Uri
* @return String
*/
public String getVideoPath(Uri uri) {
String[] projection = {MediaStore.Video.Media.DATA};
try (Cursor cursor = getContentResolver().query(uri, projection, null, null, null)) {
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
}
/**
* Get Image Path from Image Uri
* @return String
*/
public String getImagePath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
try (Cursor cursor = getContentResolver().query(uri, projection, null, null, null)) {
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment