android get video thumbnail PATH, not Bitmap - java

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

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

Adding video thumbnail in the app causes lag

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

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

how to convert `content://media/external/images/media/Y` to `file:///storage/sdcard0/Pictures/X.jpg` in android?

I'm trying to upload image to Google Drive from my android app,
based on this tutorial.
When I debug their sample project I see a typical fileUri =
file:///storage/sdcard0/Pictures/IMG_20131117_090231.jpg
In my app I want to upload an existing photo.
I retrieve its path like that
private void GetAnyImage()
{
File dir = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Pictures/Screenshots");
// --> /storage/sdcard0/Pictures/Screenshots
Log.d("File path ", dir.getPath());
String dirPath=dir.getAbsolutePath();
if(dir.exists() && dir.isDirectory()) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
startActivityForResult(intent,REQUEST_ID);
}
}
and eventually get this typical fileUri =content://media/external/images/media/74275
however when running this line of code
private void saveFileToDrive() {
// progressDialog = ProgressDialog.show(this, "", "Loading...");
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
I get an error:
java.io.FileNotFoundException: /external/images/media/74275: open failed: ENOENT (No such file or directory)
how can I solve this?
how to convert content://media/external/images/media/Y to file:///storage/sdcard0/Pictures/X.jpg ?
Will something like this work for you? What this does is query the content resolver to find the file path data that is stored for that content entry
public 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();
}
}
}
This will end up giving you an absolute file path that you can construct a file uri from
If you just want the bitmap, This too works
InputStream inputStream = mContext.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
if( inputStream != null ) inputStream.close();
sample uri : content://media/external/images/media/12345

Categories