Created
July 24, 2012 21:22
-
-
Save jdeloach/3172742 to your computer and use it in GitHub Desktop.
File Path to Android Content URI, credit to http://stackoverflow.com/a/11603841/1079757. Some modifications to use as an image and more polished this way. Took forever to find. Useful for working with the Android Gallery App
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
/** | |
* Returns the Uri which can be used to delete/work with images in the photo gallery. | |
* @param filePath Path to IMAGE on SD card | |
* @return Uri in the format of... content://media/external/images/media/[NUMBER] | |
*/ | |
private Uri getUriFromPath(String filePath) { | |
long photoId; | |
Uri photoUri = MediaStore.Images.Media.getContentUri("external"); | |
String[] projection = {MediaStore.Images.ImageColumns._ID}; | |
// TODO This will break if we have no matching item in the MediaStore. | |
Cursor cursor = getContentResolver().query(photoUri, projection, MediaStore.Images.ImageColumns.DATA + " LIKE ?", new String[] { filePath }, null); | |
cursor.moveToFirst(); | |
int columnIndex = cursor.getColumnIndex(projection[0]); | |
photoId = cursor.getLong(columnIndex); | |
cursor.close(); | |
return Uri.parse(photoUri.toString() + "/" + photoId); | |
} |
Try this. Tested Android P, PH-1
File f = new File(resource1.getResource());
Uri contentUri = Uri.fromFile(f);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to get the file path to content uri in ionic 3?