Android: Creating video thumbnails from Video URI - android

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

Related

Error getting image path selected by Gallery Intent (Android 6 - Some devices)

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.

How to get a File from a URI?

I select an image from the image gallery in my app. In the onActivityResult it returns an Intent object and I execute the getData() method on it. I get the image URI back. I am now trying to convert that in to a File so I can send it to AWS S3.
This is what I have been trying but it just returns null:
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
I also try
File file = new File(imageURI.getPath())
also null. Is this impossible or can somebody help?
supposing your onActivityResult checks in with the Intent object being ReturnedIntent you can do
File f = new File(ReturnedIntent.getData());
or get an inputstream and use it
getContentResolver().openInputStream(ReturnedIntent.getData())
//returns inputstream
what is the essence of this String object String filePath ?

Convert real path format to Uri in 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

android close managed cursor

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

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

Categories

Resources