Hi am working on a video app in android i want to generate video thumbnail and send to the server or simple how can i get video thumbnail and store in server so that when i retrieve the video i can also get the video thumbnail to use in a recycle view thanks
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable bitmapDrawable = new BitmapDrawable(thumb);
vidPreview.setBackgroundDrawable(bitmapDrawable);
I assume you are sending the video to the server also? If so then it may be better to generate the thumbnail on the server as you usually have more processing power there and less worry about consuming battery. It also saves you having to send the generated thumbnail to the server.
If you do want to create the thumbnail on the Android device then the following code will work (before this chunk the app has loaded all the videos in Media Store using the loader pattern and they are accessible via the 'cursor' variable below) - see the 'getThumbnail' method call:
while (videoCursor.moveToNext()) {
//Create the Thumbnail for this video
Log.d("ItemListFragment", "onLoadFinished: Creating Thumbnail");
String videoTitle = videoCursor.getString(titleColumn_index);
String videoPath = videoCursor.getString(pathColumn_index);
long videoID = videoCursor.getLong(idColumn_index);
Bitmap thisVideoThumbnail = MediaStore.Video.Thumbnails.getThumbnail(this.getActivity().getContentResolver(), videoID, MediaStore.Images.Thumbnails.MINI_KIND, null);
if (thisVideoThumbnail == null) {
Log.d("VideoContent refresh ","VideoThumbnail is null!!!");
}
VideoItem newVideoItem = new VideoItem(videoID, videoTitle, videoPath, thisVideoThumbnail);
//Add the new video item to the list
videosArray.addItem(newVideoItem);
}
Related
Scenario: I am using MessagingStyle notification in a chat application. For image messages, I am using setData function to create image notification in conversation style.
public Message setData(String dataMimeType, Uri dataUri)
This method required dataUri but I have URL of image so I have created a bitmap from image url and then create a uri from batmap using function below.
public static Uri getImageUri(Context applicationContext, Bitmap photo) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(), photo, "IMG_" + Calendar.getInstance().getTime(), null);
if (path == null) {
return null;
}
return Uri.parse(path);
} catch (SecurityException ex) {
ex.printStackTrace();
return null;
}
}
Problem: Now the problem is that when user open image gallery, these notifications images are showing because I used mediastore to create URI. I don’t want these image to show in gallery.
Limitation: I am also saving that URI in case If I have to generate this notification again. So I can’t delete that image immediately to avoid showing in gallery.
Question: Is there any way to save image privately in external storage where gallery don’t have access. But notification manager can access?
Is there any way to create Uri form url without downloading it? I have tried this but it also not working
url = new URL(messageNotification.getAttachmentImage());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
imageUri= Uri.parse(uri.toString());
Another workaround to show image in notification by using Messagings style like conversation as in WhatsApp
Download your image directly from url to a file in getExternalFilesDir(). Dont use an intermediate bitmap.
The MediaStore has no access to your getExternalFilesDir() so Gallery apps dont know about your files there.
The user can still use the Files app to see those images.
Then use a FileProvider to serve your files. Construct an uri with FileProvider.getUriForFile().
store your bitmap as File in some app-specific directory (not gallery) and make URI from file path. be aware of Scoped Storage and pick proper place for storing Bitmaps - check out in HERE, probably getFilesDir()/getCacheDir() method will be the best option
also worth trying may be this line:
Uri uri = Uri.parse(messageNotification.getAttachmentImage());
(encodedImageUrl = URLEncoder.encode(imageUrl, "utf-8") may be needed)
I'm building a music player in Android. I'm displaying the album art for currently playing song. This is my code to retrieve the embedded image:-
public Bitmap getAlbumArt(String filePath) {
Bitmap bm = null;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);
byte [] data = mmr.getEmbeddedPicture();
// convert the byte array to a bitmap
if(data != null) {
bm = BitmapFactory.decodeByteArray(data, 0, data.length);
}
return bm;
}
This code works fine and is able to retrieve album art but only for some songs. However, I can see the album art for all the songs in thumbnail view in Windows.
Could somebody explain what might be the reason behind this? Also, if there is an alternate way to achieve the same?
Thank You!
I'd like to load image which is on SDCARD in folder to imageView of my cell in listView. I've tried different solutions but each of them fails. When I load images normally as it is in every base tutorial everything works. I've observed that, my application slows down when it has to load many images. Images are taken from photo camera of device. I'd like to load each of them asynchronously to avoid UI slow reaction. I've tried to use Thread, Asynctask but each of them throws error: "Only the oryginal thread that created a view hierarchy can touch its views". How to load images to avoid speed problems? SDImageLoader is a class which is possible to get from GITHUB. What I've tried is a standard code but is slows:
In a getView method in ListAdapter:
File imageFile = new File(Options.PATH_TO_FOLDER_PHOTOS_OF_APP + "test.png");
String imageFileString = Options.PATH_TO_FOLDER_PHOTOS_OF_APP + "test.png";
// "test.png" is a test file. Each cell will have different name of file to load.
if(imageFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
image.setImageBitmap(myBitmap);
// final SDImageLoader loader = new SDImageLoader(context);
// new SDImageLoader().load(imageFileString, image);
//UrlImageViewHelper.setUrlDrawable(image, url);
}
else
{
final SDImageLoader loader = new SDImageLoader();
Resources resources = context.getResources();
int resurceId;
resurceId = resources.getIdentifier("bad", "drawable",context.getPackageName());
loader.load(imageFileString, image);
image.setImageResource(resurceId);
}
Have you tried to refresh your project after adding an external library to your project? It doesn't matter with the fragment. You send exact context to the List Adapter - which should be fragment.this.getActivity().
I really need some help please!
I want to make a MP3 Player on Android and therefore I need to find all MP3 files in the storage.
So I search them recursively in my get MP3 method:
private void getMP3Files(Context context, String directory,ArrayList<MusicListArray> mp3_list){
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
Uri uri;
byte[] album_art;
Bitmap bitmap;
/*File[] files = Directory.listFiles(new MP3FileNameFilter());
files = Directory.listFiles();*/
File folder = new File(directory);
for (File file : folder.listFiles()) {
if (file.isFile()) {
if (file.getName().endsWith(".mp3") || file.getName().endsWith(".MP3")) {
uri = Uri.fromFile(file);
mediaMetadataRetriever.setDataSource(context,uri);
String artist = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String title = file.getName();
album_art = mediaMetadataRetriever.getEmbeddedPicture();
if(album_art != null){
bitmap = BitmapFactory.decodeByteArray(album_art, 0, album_art.length);
}else{
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
}
if(bitmap == null){
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
}
if (title.indexOf(".") > 0)
title = title.substring(0, title.lastIndexOf("."));
if(artist == null){
artist = getString(R.string.unknown_artist);
}
mp3_list.add(new MusicListArray(title,artist,file,bitmap));
}
}else if(file.isDirectory()){
getMP3Files(context, file.getPath(), mp3_list);
}
}
Collections.sort(mp3_list);
/*for(int i=0;i<files.length;i++){
mp3_list.add(new MusicListArray(files[i].getName(),"Test",files[i]));
}*/
//return mp3_list;
}
This is very slow and needs about 3 seconds everytime I start my app.
First question: How do I manage to reduce the taken time?
Second question: How to save the list in a file and load it?
Thanks in advance!
I suggest you use an SQL database to store the metadata (including file location) of your mp3s. I should warn you that I have no experience with media on Android though. Basic Idea:
1) First Time use (or on a manual sync in settings) do a full 3 second sync -> look at SharedPreferences to store a key indicating first time use for your app
2) Store this meta information in a SQL database.
3) Make the app read from the database as this is much faster, especially if the table is designed correctly. Store things like cover art, file location, artist, title etc. You can also add things like number of times listened and stuff. In addition if you wrap your SQL layer into a content provider you can perform custom searches of all your music.
4) When app is running, run a background indexing service that finds new music, corrects changes/deletes etc. Look into android Service
The benefits of this is that your app will be much faster to the user, only need to load all the data once in the foreground and provide you with a lot more flexibility in terms of searching, song choice, custom data etc.
Is it possible to get a cover picture by song and not by album.
Because I have one self combined album with songs and they all have different cover pictures.
But when I want to query them I always get the same picture returned.
String[] ARG_STRING = {MediaStore.Audio.Media.ALBUM_ID};
...
String albumCover = _cursor.getString(_cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
...
MusicUtils.getArtwork(this, -1, Integer.parseInt(albumID));
So i would like to know how it's possible to get an cover image of an song.
I know MusicUtils supports getArtwork by SongId, but what ID should I use because MediaStore.Audio.Media._ID is not working.
I'm not familiar with MusicUtils, however, you should be able to get the cover art from the file itself by using MediaMetadataRetriever. Here is a brief code snippet showing how to use it. The uri referenced is the content uri for the file you want to retrieve the art for.
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt;
Bitmap art;
BitmapFactory.Options bfo=new BitmapFactory.Options();
mmr.setDataSource(getApplicationContext(), uri);
rawArt = mmr.getEmbeddedPicture();
// if rawArt is null then no cover art is embedded in the file or is not
// recognized as such.
if (null != rawArt)
art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
// Code that uses the cover art retrieved below.