Adding video thumbnail in the app causes lag - java

I am working on a video player app but when add a video thumbnail in the app, it becomes laggy.
here is my code
videofielAdapter.java
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoList.get(position).getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);
holder.VideoThumb.setImageBitmap(bitmap);
MainActivity.java
private ArrayList<Videos_Models>LoadVideos(Context context) {
ArrayList<Videos_Models> TempList = new ArrayList<>();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String [] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.DATE_ADDED,
MediaStore.Video.Media.DURATION,
};
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor!= null){
while (cursor.moveToNext()){
String id =cursor.getString(0);
String path =cursor.getString(1);
String title =cursor.getString(2);
String fileName =cursor.getString(3);
String size =cursor.getString(4);
String dateAdded =cursor.getString(5);
String duration =cursor.getString(6);
}
}
}

Related

I am trying to get all the audio files from my internal and external storage

I use media Store to get All the files from the Storage but my code didn't work . help me to solve issue. I tried all the tricks from the internet but also after that i am not able to solve this..
enter code here
public List<Song> getAllAudioFromDevice(final Context context) {
final ArrayList<Song> tempAudioList = new ArrayList<Song>();
File temp= (File)Environment.getExternalStorageDirectory();
String selection=MediaStore.Audio.Media.IS_MUSIC+"!=0";
Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
Log.d("file",temp.toString());
String[] projection = {MediaStore.Audio.Media._ID,
// MediaStore.Audio.Media.TITLE,
// MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST
// MediaStore.Audio.Media.ALBUM_ID
};
Cursor c = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection,null,null,null);
if (c != null) {
do {
Song audioModel = new Song();
int path =(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
int name =c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
int album =(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
audioModel.setaName(c.getString(name));
Log.e("Uri",(c.getString(name)));
audioModel.setaAlbum(c.getString(album));
audioModel.setaPath(c.getString((path)));
Log.e("Name :", audioModel.toString());
tempAudioList.add(audioModel);
}while (c.moveToNext());
c.close();
}
Log.e("N", Integer.toString(tempAudioList.size()) );
return tempAudioList; }

Auto retrieve image from Download folder and show in ImageView

I have a Download folder in which there are many images. I want to get a single image from that folder and show into ImageView. Code is given below I am using this code to onCreate() method. So that there is no need to choose from the folder. I have already given required permission. But getting java.io.FileNotFoundException: Multiple items at content://media/external/images/media at given code.
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg);
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Uri selectedImage = intent.getData();
String[] filePaths = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePaths, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePaths[0]);
String picturePath = c.getString(columnIndex);
Log.e(TAG, "onActivityResult: "+picturePath );
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
try {
Uri selectedimg = intent.getData();
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg);
Bitmap resizedBmp = Helpers.resizeForPreview(image);
selectImage.setImageBitmap(resizedBmp);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("path of image ", picturePath + " "+ Uri.parse(picturePath));
selectImage.setImageBitmap(thumbnail);

Fetching exact path of songs and building a URI

I am using Exo Player in order to make an audio player for android. So far i am using this code and fetching the following info from my phone. Data, Name, Artist.
public void getMp3Songs() {
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
int songId = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
arrayList.add(new Songs(songId, name, url));
} while (cursor.moveToNext());
}
cursor.close();
}
}
The next thing i need is exact path of these tracks.
For that purpose i am using this code.
private void initializePlayer(){
player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector()
, new DefaultLoadControl());
simpleExoPlayerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow,playbackPosition);
Uri uri = Uri.parse(url);
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
}
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource(uri,
new DefaultHttpDataSourceFactory("ua"),
new DefaultExtractorsFactory(), null, null);
}
The problem is that when i start the player, nothing happens. Its just a blank player and its not playing anything. I think i am messing with the file paths here. Please help.
Okay so i found the solution if someone is in need he can use it. The problem was not in getting the address or building the Uri. It was in this particular section. new DefaultHttpDataSourceFactory("ua") Change this to this:
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "idk"), new DefaultBandwidthMeter());
Now use this instance while returning MediaSource.

Get new Uri from a moved file

I'm doing an image picker in Android. I pick some photos from gallery and after I move those photos to another directory. When I pick those photos I get all information about them (Uris, realPath, if is portrait or not...), now, when I move these photos to another directory I suppose the uri will be changed, right? How can I get the new Uri? I know that will be something like that, but I'm not sure because I don't understand the whole code.
I don't know when I have the Uri's photos moved how to get the new ones. Thanks
protected ArrayList<Image> doInBackground(#NonNull Void... voids) {
ArrayList images = new ArrayList();
Cursor imageCursor = null;
try {
String[] e = new String[]{"_id", "_data", "date_added", "height", "width"};
String orderBy = "date_added DESC";
imageCursor = GalleryActivity.this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, e, (String)null, (String[])null, "date_added DESC");
while(imageCursor.moveToNext()) {
long _id = imageCursor.getLong(imageCursor.getColumnIndex("_id"));
int height = imageCursor.getInt(imageCursor.getColumnIndex("height"));
int width = imageCursor.getInt(imageCursor.getColumnIndex("width"));
String imagePath = imageCursor.getString(imageCursor.getColumnIndex("_data"));
Uri uri = Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, String.valueOf(_id));
Image image = new Image(_id, uri, imagePath, height > width);
images.add(image);
}
} catch (Exception var16) {
var16.printStackTrace();
this.error = var16.toString();
} finally {
if(imageCursor != null && !imageCursor.isClosed()) {
imageCursor.close();
}
}
return images;
}

android get video thumbnail PATH, not Bitmap

Is it possible to get the video thumbnail PATH, not Bitmap object itself? I'm aware of method
MediaStore.Images.Thumbnails.queryMiniThumbnail
but since I use my own Bitmap caching mechanism I want to have the path to video thumbnail rather than the Bitmap object itself. This method returns Bitmap object, not path.
Thanks
First get the video file URL and then use below query.
Sample Code:
private static final String[] VIDEOTHUMBNAIL_TABLE = new String[] {
Video.Media._ID, // 0
Video.Media.DATA, // 1 from android.provider.MediaStore.Video
};
Uri videoUri = MediaStore.Video.Thumbnails.getContentUri("external");
cursor c = cr.query(videoUri, VIDEOTHUMBNAIL_TABLE, where,
new String[] {filepath}, null);
if ((c != null) && c.moveToFirst()) {
VideoThumbnailPath = c.getString(1);
}
VideoThumbnailPath, should have video thumbnail path. Hope it help's.
Get Video thumbnail path from video_id:
public static String getVideoThumbnail(Context context, int videoID) {
try {
String[] projection = {
MediaStore.Video.Thumbnails.DATA,
};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Video.Thumbnails.VIDEO_ID + "=?",
new String[] { String.valueOf(videoID) },
null);
cursor.moveToFirst();
return cursor.getString(0);
} catch (Exception e) {
}
return null;
}

Categories