Created
December 16, 2019 11:31
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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