I tried to create a video thumbnail as described here.
I also read the reference here.
In my app I first let the user choose a video with:
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("video/*"), ACTIVITY_PICKVIDEO);
Then I determine the video ID with:
fileID = Integer.parseInt(contentUri.getLastPathSegment());
So, the video content://media/external/video/media/5 would have the ID 5.
Then I try to get the thumbnail bitmap with:
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, fileID, MediaStore.Video.Thumbnails.MICRO_KIND, options);
There's no exception thrown but the bitmap has a width and height of -1.
I'm not sure if the ID needed in getThubnail() is actually the ID that I determined above.
Does anyone know of a working example how to get the thumbnail bitmap if you have the content Uri?
Interestingly (maybe so) I get null when trying with MediaStore.Video.Thumbnails.MINI_KIND as thumbnail size and an IllegalArgumentException ("Unsupported kind: 2") when I try FULL_SCREEN_KIND.
I'm using a Motorola Milestone with Android 2.1.
EDIT:
I also tried getting the ID with querying for the BaseColumns._ID but it turns out to be the same as in the Uri (in the given example the _ID is 5).
for getting video id try this
String[] proj = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DATA
};
Cursor cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, MediaStore.Video.Media.DISPLAY_NAME+"=?",new String[] {"name.mp4"}, null);
cursor.moveToFirst()
fileid = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));
for getting thumbnail :
ContentResolver crThumb = getContentResolver();
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb,fileid, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv2.setImageBitmap(curThumb);
here iv2 is imageview and name.mp4 will represent your file name
Related
I am trying to get the image of mp3 file on my phone. I've been struggling a few days and now I know;
Firstly, I use a content provider with this code
` Uri allSongsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ALBUM_KEY,
MediaStore.Audio.Media.DATE_MODIFIED
};
String sortOrder = MediaStore.Video.Media.DATE_MODIFIED + " DESC";
And I create my cursor like this;
Cursor cursor = ctx.getContentResolver().query(allSongsUri, projection, null, null, sortOrder);
In there I have all my mp3 files. That's ok. Now I want to get their's cover image.
If I use MediaMetadataRetriver's getEmbeddedPicture method like this
mediaMetadataRetriever.setDataSource(localeMusic.getPath());
localeMusic.setImage(mediaMetadataRetriever.getEmbeddedPicture());
it took about 6-7 seconds with nearly 40-50 file. Because getEmbeddedPicture() is too slow!.
Also tried to get album_id and query another cursor to MediaStore.Audio.Albums and I saw all album id is the same because all of them is the same folder. I don't want to show their Album image. I want to show the file's cover image.
I tried this like here But in there, we are getting file's albums image. That is not I ask.
Where can I find the file's image path? Where can the MetadataRetriever class get it with getEmbeddedPicture? Where is that embedded picture?
Thanks. Best regards
mediaMetadataRetriever.getEmbeddedPicture() is a native method and every native method call has a price because JNI calls takes time. So, if you iterate such calls, you may lost a lot of time. You may try to call this method asynchronously on demand and cache the result. Try to combine with glide
This will solve two of your problem at the same time, glide calls is naturally asnyc, so you don't need to implement an AsyncTask class also it caches your images.
This is the basic glide usage:
Glide.with(context).load(mediaMetadataRetriever.getEmbeddedPicture());
I encourage you to read glide documents. It has lots of other features.
Hope this helps.
You can get the album art as mentioned here
https://stackoverflow.com/a/17574629/3825975
Basically,
Cursor cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID+ "=?",
new String[] {String.valueOf(albumId)},
null);
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
// do whatever you need to do
}
But to get individual song art, this is how I remember doing it. Hope it helps.
float ht_px = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
Bitmap artwork, bitmap2 = null;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt;
BitmapFactory.Options bfo = new BitmapFactory.Options();
try {
mmr.setDataSource(songUri); //songUri is the uri of the song. You need to extract this from the song id
rawArt = mmr.getEmbeddedPicture();
if (rawArt != null)
bitmap2 = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
if (bitmap2 == null)
bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.def_icon);
artwork = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true); //This is the bitmap you need
} catch (Exception e) {
//Exception handling
}
}
I want to select a video from my Gallery. It's working fine. But now I want to display a Bitmap, as a thumbnail.I tried this code and it's not working, it always says: NullPointerException
Bitmap bitmap2 = ThumbnailUtils.createVideoThumbnail(uri.getPath, MediaStore.Video.Thumbnails.MICRO_KIND);
This is all in an onActivityResult().
How can I get the Bitmap from the video Uri??
Thanks for your help
In the latest API 24, you may face some issues if you stick with an approach in the accepted answer.
for example in this line int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); sometimes I got W/System.err: java.lang.IllegalArgumentException: column '_data' does not exist error message.
also in the latest API, you may get SecurityException if you deal with widgets or shared content. Keep that in mind.
As for the video thumbnail from Uri - I use an approach which utilizes MediaMetadataRetriever, thus you don't need to get String filePath:
MediaMetadataRetriever mMMR = new MediaMetadataRetriever();
mMMR.setDataSource(context, videoUri);
bmp = mMMR.getFrameAtTime();
Hope this helps
in onActivityResult
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(picturePath, MediaStore.Video.Thumbnails.MICRO_KIND);
Edit
Kotlin version
val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
val cursor = context.contentResolver.query(uri, filePathColumn, null, null, null)
cursor.moveToFirst()
val columnIndex = cursor.getColumnIndex(filePathColumn[0])
val picturePath = cursor.getString(columnIndex)
cursor.close()
val bitmap = ThumbnailUtils.createVideoThumbnail(picturePath, MediaStore.Video.Thumbnails.MICRO_KIND)
Try this one:
Bitmap bitmap2 = ThumbnailUtils.createVideoThumbnail( uri.getPath() , MediaStore.Images.Thumbnails.MINI_KIND );
For API 27, for document URI (1000 is microseconds)
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource( context, doc_uri );
Bitmap bm = mmr.getScaledFrameAtTime( 1000, MediaMetadataRetriever.OPTION_NEXT_SYNC, 128, 128 );
This works for me:
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);
Using ThumbnailUtils, you can create thumbnail of two types.
MediaStore.Images.Thumbnails.MICRO_KIND - type will generate thumbnail of size 96 x 96.
MediaStore.Images.Thumbnails.MINI_KIND - type will generate thumbnail of size 512 x 384.
createVideoThumbnail() needs the file path, not the content uri.
The file path requires external read permissions.
If you're getting null responses, it may be from using the content uri (though the assumption in ThumbnailsUtils.java is of a corrupt video file). When I fixed that, then I was getting permissions errors.
I was able to get the file path from the content uri using the video's ID like this:
val selection = MediaStore.Video.Media._ID + " = $id"
val cursor =
this.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,
selection, null, null)
And then continue on with the cursor as in the other answers in SO.
Docs for contentResolver.query()
Cursor c = MediaStore.Video.query(cr,uri, new String[]{
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.DATA});
if (c!=null){
c.moveToFirst();
int id = Integer.valueOf( c.getString(0) );
c.close();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
try {
return MediaStore.Video.Thumbnails.getThumbnail(cr, id, MediaStore.Video.Thumbnails.MINI_KIND, options);
}catch (java.lang.SecurityException ex){
ex.printStackTrace();
//TODO: add create ThumbnailUtils.createVideoThumbnail
return null;
}
}
CancellationSignal ca = new CancellationSignal();
var vthumb = ThumbnailUtils.createVideoThumbnail(new File(value),new Size(120,120), ca);
this used to work and now not anymore. Don't get why.
I want to get reference to a photo from my android phone:
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
BitmapFactory.Options options = new BitmapFactory.Options ();
options.inSampleSize = 4;
// case 1
final String fname = new File(getActivity().getFilesDir(), imgDecodableString).getAbsolutePath();
Bitmap fBitmap = decodeFile(new File(getActivity().getFilesDir(), imgDecodableString));
// case 2
Bitmap tempBitmap = BitmapFactory.decodeFile(imgDecodableString, options);
And while debugging:
fname: /data/data/com.test.app/files/storage/emulated/0/Download/A-small-kitten-in-a-Christmas-cap-HD-wallpaper_960x800-1.jpg
imgDecodableString: /storage/emulated/0/Download/A-small-kitten-in-a-Christmas-cap-HD-wallpaper_960x800-1.jpg
Bitmap object is always null...
I need a new pair of fresh eyes on this, please. Thank you.
My bad, i deleted the storage permission. Was looking in the wrong place. So the above code works in addition with
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Thanks.
Hi I am trying to save an image to the .thumbnails folder as shown below:
bitmap = ThumbnailUtils.extractThumbnail
(BitmapFactory.decodeFile(actualImagePath,options), 120, 120);
bitmapGenerated = true;
File file = new File(Environment.getExternalStorageDirectory()
.toString()+"/"+"DCIM/.thumbnails/"+id+".jpg");
boolean bcc =file.createNewFile();
boolean success = bitmap.compress(Bitmap.CompressFormat.PNG,60,new
FileOutputStream
(file));
The 'id' is actually the id of the particular image that I am trying to save and is obtained from a cursor as shown:
image_column_index = mCursor.getColumnIndex(MediaStore.Images.Media._ID);
id = mCursor.getLong(image_column_index);
The image gets saved in the '.thumbnails' , however, as I try to access the image thumbnail that I just created, it is not read.
I think it was because of the name I gave to the thumbnail image.
So my question is it okay to save to '.thumbnails' folder and if so in what name should we save the file?
Help is really appreciated,
Thank You.
I found out that to generate a thumbnail we don't need to explicitly create a thumbnail image file and put it in the .thumbnials folder.
There is a method called getThumbnail in MediaStore.Images.Media that we can use for our purpose. Code is as shown:
Cursor mCursor;
mCursor = mContext.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
null, null, null);
int image_column_index = mCursor.getColumnIndex(MediaStore.Images.Media._ID);
long id = mCursor.getLong(image_column_index);
MediaStore.Images.Thumbnails.getThumbnail(mContext
.getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null);
The above code will generate a thumbnail for the associated imageid , first we have to make sure that the thumbnail doesn't exist and create a new one only if one doesn't exist.
Can i get an image from SD card and store it in the application database??
Is this idea effective in my application??
Thanks in advance!!
Regards
Yes you can get the image from your sd card and store it in sqlite database, we have done it but it will be too expensive for your device....... So be careful about that,
call image picker intent by calling this
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, 12);
It will open default gallery picker view , when you select an image you will be returned in your current activity, So override onActivityResult() and look for result code 12.
in that code you can get cursor by these lines
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
From that cursor object you can get the file path of image by these lines
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Here is the bitmap object
Bitmap bMap = BitmapFactory.decodeFile(filePath);
Get byte arrays
ByteArrayOutputStream out = new ByteArrayOutputStream();
bMap.compress(Bitmap.CompressFormat.PNG, 100,out);
byte[] imageArray = out.toByteArray();
Now you have to store imageArray as blob in your database. And you are done.... Don't forget to refer this post from where i got the solution...