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);
}
Related
I am trying to get the path of an image when the user pick from the gallery (with intent)
It's been working ok, since some users noticed that could not do it witn Android 6.0.
I have tried different things, and some solutions works in the emulator with Android 6.0 but not in my Xiamoi with Android 6.1.
This both solutions works in the emulator (6.0) and Android 4.4 physycal device.
public String getRealPathFromURI(Activity context, Uri contentURI) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = context.managedQuery(contentURI, projection, null,
null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
String s = cursor.getString(column_index);
// cursor.close();
return s;
}
// cursor.close();
return null;
}
and the other similar:
private static 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();
}
}
}
But in my Xiaomi 6.1 the cursor is null. But I can get the real path from:
private static String getRealPathFromURI(Context context, Uri contentUri) {
return contentUri.getEncodedPath();
}
Any help?
Thank you!
EDIT:
I'm asking for choose an image in this way:
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK); //ACTION_GET_CONTENT
// Always show the chooser (if there are multiple options available)
launchForResult(Intent.createChooser(intent, "Select Picture"), SELECT_FILE);
I'm asking for choose an image in this way:
First, use ACTION_GET_CONTENT to pick by MIME type.
Second, whatever activity that responds to ACTION_GET_CONTENT (or ACTION_PICK) does not need to return a Uri that the MediaStore knows about. In fact, most will not. They can return a Uri that points to anything, including things that are not files.
So, get rid of all of your broken getRealPathFromURI() code. Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri, and use that stream.
I am dealing with camera images in my app which actually get as urls.The camera app is in portrait mode.So the images are sometimes left rotated or right rotated.I know that we can use EXIF for normal image orientation from gallery, there we can check the exif value and do appropriate changes.Basicaly this is the code for exif
ExifInterface exif = new ExifInterface("filepath");
exif.getAttribute(ExifInterface.TAG_ORIENTATION);
So my question is what is the filepath in my case, is it the url itself or should i create a file for that..
Any help will be greatly appreciated ....
First, you have to get Uri in onActiviryResult, like here
Get image Uri in onActivityResult after taking photo?
Then, when you have Uri you can get filepath like this:
String filepath = getRealPathFromURI(someUri);
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if(cursor != null && cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
Then, if filepath is not null and not empty, you can get EXIF data, check it and rotate image if it needs.
Hope it will help.
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
I have a code which takes images from gallery using gallery intent and data is send to my activity as an uri like:
content://media/external/images/media/338
I need to get the path of this picture and I am using
public String getRealPathFromURI(Uri contentUri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, projection, null, null, null);
try {
cursor.moveToFirst();
return cursor.getString(0);
} finally {
cursor.close();
}
}
but this code causes a crash on android ICS when trying to close a managed cursor. This is a known issue (see issue). Is there a way to close this cursor without causing a crash? I don't want to exit this method without closing the cursor. I need to suport android 2.1 and up. Thanks
found it:
public String getRealPathFromURI(Uri contentUri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
try {
cursor.moveToFirst();
return cursor.getString(0);
} finally {
cursor.close();
}
}
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);
}