Convert content:// uri into a file path - android

I am having problems converting a uri to a path because of the content tag. So I am trying to select any file type from the Android storage, and I am able to select a file but when I grab the data and try to convert it to a string path my app crashes.
My code to convert the uri looks like
String path = data.getData().getPath();
I've looked around and some say to use a Content provider and content resolver, but I'm not sure how to use them. Any help would be great thanks.
Display it/ upload it to an s3 bucket. When I mean display it, I mean if it's a photo, or video to show it and if it's an audio file I'd like to be able to play it and the same with other files, like PDF and so on.

This method returns the Path as String
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Related

How to get path of media files in android

I want paths of files shared through share intent from any app. I do get path of files like zip, apk, pdf, etc. with code ShareCompat.IntentReader.from(this).getStream().getPath();.
But but I don't get path for image, video and audio files I get content://media/external/video/media/40666, How to get real path of files of this type?
If all you want is the path, you can use the following code
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":")+1);
cursor.close();
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null
, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
It basically uses he ContentResolver to get the data of the file.
You can change the query parameters to get what you want.
Or you can use
getContentResolver().openInputStream(uri)
If you want the entire file

Get filename and path from URI from mediastore in Nought and Oreo device

I have image uri like this :"content://com.iceburgapp.provider/external_files/.pkgName/File1.jpg"
Now whenever I get path it give me : "/external_files/.pkgName/File1.jpg"
I want to get RealPathFrom content Uri.
I got solution from stackoverflow and I tried below code but not working for me :
public String getRealPathFromURI(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();
}
}
}
AnyOne know How to do this? I got below error for using above code.
java.lang.IllegalArgumentException: column '_data' does not exist. Available columns: []
It's only not working in the Nought and oreo device because of contentUri using file provider.
Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Ideally, just use that stream directly, for whatever it is that you are trying to do. Or, use that InputStream and some FileOutputStream on a file that you control to make a copy of the content, then use that file.
This is working properly in devices before Android N

Intent.ACTION_GET_CONTENT opens recent files which gives a bad URI

I'm using Intent.ACTION_GET_CONTENT which opens recent files. Selecting items from the recent files gives a bad URI but selecting the same file from the file manager gives a right URI which can be handled by my code.
public static String getRealPathFromURI(Context context, Uri uri) {
String path;
if ("content".equals(uri.getScheme())) {
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
path = cursor.getString(idx);
cursor.close();
} else {
path = uri.getPath();
}
return path;
}
Note: The uri.getPath() output when I select a PDF from the recent files is /document/... but selecting the same file from the file manager is, .../emulated/....
Note: the error while selecting the file from the recent files is
Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
The problem was my code doesn't handle the new Layout Storage URIs of Android. If you face this problem too, please refer to this link because the writer is wrote a fantastic method to get real path of every URIs.

How to save image in binary formate into database and also read data from database in android?

I want take image from camera then save image in SD card folder but path is save in SqLite database and also save image in binary format. Also when read from database then show image in ImageView or GridView. any have resource or example or tutorial links about this. If anybody give me any resource, or example or tutorial links. It will be very helpful for me.
You can split your task in three independent parts:
1. Take a picture from camera
2. Save it to a file and take the path
3. Store the path from point two in your database
For part 1 and 2:
final int IMAGE_FROM_CAMERA = 0;
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, IMAGE_FROM_CAMERA);
In onActivityResult()...
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String path = getRealPathFromURI(selectedImage);
}
...
public String getRealPathFromURI(Uri contentUri) {
String path = null;
String[] proj = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null,
null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
path = cursor.getString(column_index);
}
cursor.close();
return path;
}
For part 3 just write the string path the appropriate field in your database. For opening is easy, since you have the path, just open a stream from the path, do some conversions and get your image.

Android working with MediaStore

How can I open a file that I stored in gallery? I used this method to put my file there:
MediaStore.Images.Media.insertImage()
According to documentation it returns String url to the newly created file. However when I trying to open this file it fails. Getting file not found exception.
What am I missing? Any ideas?
It returns the String url or the content uri string back. To get the actual file path from the uri you need to use something like
protected String convertMediaUriToPath(Uri uri) {
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
You can convert the string url that you get to the Uri using Uri.parse(url);

Categories

Resources