Convert real path format to Uri in android - android

for example
real path is mnt/sdcard/image_1.jpg
Uri path is content://media/external/images/media/140 like this
Uri photoUri =Uri.parse("content://media/external/images/media/140");
Log.d("selectedphoto",""+photoUri);
selectedImagePath = getPath(photoUri);
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
In above code I convert Uri to real path but dnt know how to convert real path to Uri

Try this:
Uri.fromFile(new File("/sdcard/cats.jpg"));

This will get the file path from the MediaProvider, DownloadsProvider, and ExternalStorageProvider, while falling back to the unofficial ContentProvider method you mention.
https://stackoverflow.com/a/27271131/3758898

Related

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

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);

How to change file URI string

How to change something file:///system/media/lockscreen/lockscreen_001.jpg to
something like /mnt/sdcard/myPicture.jpg
The reason why I want to change is that file:/// is wrong if I want to further process. It is hard to tell but if I get the URI from Uri uri= data.getData(); is file:///system/media/lockscreen/lockscreen_001.jpg, how to handle because normally is start with mnt
Try this :
Uri uri = Uri.parse("file:///system/media/lockscreen/lockscreen_001.jpg");
Toast.makeText(getApplicationContext(), ""+uri.getPath(), Toast.LENGTH_LONG).show();
uri.getPath() will give you the path by eliminating file: extension.
EDIT :
Uri urinew = Uri.parse(uri.getPath());
This will give you new uri with the path you got.
Hope it helps you.
Thanks.
Hope this code can help you :
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);

How to get a name of captured image in Android

after capture the image in android how to get a name that save in SD card to programme? or how to set a name for capturing image to save in SD card. such as using incremental number.
pls give me some sample code
I found answer from here.
using this method
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Android: Creating video thumbnails from Video URI

I'm budiling an application and it lists all the videos i recorded using the recorder in a list. Is it possible for me to create a thumbnail with the help of Uri instead of the string???
my current code goes as below but it no longer works as my input to the constructor is Uri not string.
bmThumbnail = ThumbnailUtils.createVideoThumbnail(
(db_results.get(position)), Thumbnails.MICRO_KIND);
imageThumbnail.setImageBitmap(bmThumbnail);
I'm returned the error
The method createVideoThumbnail(String, int) in the type ThumbnailUtils is not applicable for the arguments (Uri, int)
Thanks for your time in advance.
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Categories

Resources