I am trying to generate a thumbnail of the video. But I cannot use Glide as it colliding with other gallery libraries.
I have tried Picasso that is not giving me thumbnail from the URL. ThumbnailUtils.createVideoThumbnail is working in my code but it is too slow.
Can someone suggest any effective tool or technique, to get the thumbnail from the URL in java??
You can use the MediaMetadataRetriever class. For example
private fun getThumbNail(uri: Uri): Bitmap {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, uri)
return retriever.frameAtTime //or frameAtPosition(..)/frameAtIndex(..)
}
Related
I wanted to know is there a way i can show my image view image in fragment.
The image i am getting is from Firebase storage and passing the bitmap value through a view Model function and including the value in the fragment.
I wanted to know is there a better way to do this?
Also when there is no image, the image still shows when i pair it up with androidswipetorefresh listener
Main Activity Code
val imageName = auth.currentUser?.uid
val storageRef = FirebaseStorage.getInstance().reference.child("profileImages/$imageName.jpg")
val localFile = File.createTempFile("tempImage","jpg")
storageRef.getFile(localFile).addOnSuccessListener {
val bitmap = BitmapFactory.decodeFile(localFile.absolutePath)
fragmentViewModel.setImage(bitmap)
}.addOnFailureListener {
Toast.makeText(this,"Failed",Toast.LENGTH_SHORT).show()
}
Fragment Code
profileImg = view.findViewById(R.id.profileImage)
val bitmap = viewModel.imageData.value
profileImg.setImageBitmap(bitmap)
View Model Code
val imageData = MutableLiveData<Bitmap>() fun setImage (newData: Bitmap) {
imageData.value = newData
}
When getting images from a specific URL, it's more likely to be used a third-party library that can do the job for you. So, I definitely recommend you to use an image loading and caching library. For Android we have Glide:
Glide is a fast and efficient open-source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy-to-use interface.
binding.btnShareOne.setOnClickListener {
val intent = Intent(Intent.ACTION_SEND).setType("image/*")
val bitmapDrawable = binding.ivLayoutOne.drawable.toBitmap()
val path = MediaStore.Images.Media.insertImage(
contentResolver,
bitmapDrawable,
"tempimage",
null
)
val uri = Uri.parse(path)
intent.putExtra(Intent.EXTRA_STREAM, uri)
startActivity(Intent.createChooser(intent, "Share Image"))
}
This is my code. I want to get the path from the bitmap and pass it to Uri. When I run my current code it gives me " java.lang.NullPointerException: uriString " error.
I have researched a bit and I think it will be solved by scoped storage but I cannot seem to implement it.
Since .insertImage() is deprecated and also Uri is returning null, this method of mine is not working to get image from image view and share it.
Please help.
A bitmap object doesn't have a path. A Bitmap object is an in memory object. Its just raw bytes. Even if it came from a file, that association isn't kept around.
I am trying to load all video files from sdcard in a recycler view. I also want the thumbnail of the video so here is my code to retrieve thumbnail of videos
var thumbColumns = arrayOf(MediaStore.Video.Thumbnails.DATA)
fun getThumbnailPathForLocalFile(fileId:Long): String?{
var thumbCursor: Cursor? = null
try {
thumbCursor = contentResolver.query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + " = "
+ fileId, null, null)
if (thumbCursor!!.moveToFirst()) {
Toast.makeText(applicationContext, thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA)),Toast.LENGTH_LONG).show()
return thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA))
}
} finally {
thumbCursor?.close()
}
return null
}
The method returns the following path /storage/emulated/0/DCIM/.thumbnails/1526834275076.jpg
Following are the ways in which I tried loading the image with glide
video.thumbnailUrl is /storage/emulated/0/DCIM/.thumbnails/1526834275076.jpg
1)
GlideApp.with(context)
.load(Uri.fromFile(File(video.thumbnailUrl)))
.into(holder.thumbnail)
2)
GlideApp.with(context)
.load("file:///"+video.thumbnailUrl)
.into(holder.thumbnail)
3)
GlideApp.with(context)
.load("file://"+video.thumbnailUrl)
.into(holder.thumbnail)
4)
GlideApp.with(context)
.load(video.thumbnailUrl)))
.into(holder.thumbnail)
Same thing is done with Picasso. I even tried using Older version of Glide but still nothing. Most of the Stack Overflow questions mentions the first way but it is not working for me. I have tried from all Android Versions from 19 to 27 but couldn't load the image.
I am getting no such file or directory for all the cases but clearly the file is present otherwise I wouldn't get the path from getThumbnailPathForLocalFile function. I have added the read storage permission. Am I fetching the video thumbnails the right way?
First use picasso latest gradle:
implementation 'com.squareup.picasso:picasso:2.71828'
Then you can simply do like this:
String filePath = "/mnt/sdcard/myimg.png";
Picasso.get().load(new File(filePath)).into(imageView);
in my android TV application, at start i want to show a frame of my each stream links. my current solution is using LoaderManager and the problem of this technique is to it's too slow(application crashed).
FFmpegMediaMetadataRetriever to load and set video stream link.
this.retriever = new FFmpegMediaMetadataRetriever();
Bitmap bitmap = null;
retriever.setDataSource(this.mSelectedStream.getStreamUrl());
bitmap = retriever.getFrameAtTime();
drawable = new BitmapDrawable(bitmap);
retriever.release();
i found this thread that explain that glide can used to load image from video links.
BitmapPool bitmapPool = Glide.get(getApplicationContext()).getBitmapPool();
int microSecond = 6000000;// 6th second as an example
VideoBitmapDecoder videoBitmapDecoder = new VideoBitmapDecoder(microSecond);
FileDescriptorBitmapDecoder fileDescriptorBitmapDecoder = new FileDescriptorBitmapDecoder(videoBitmapDecoder, bitmapPool, DecodeFormat.PREFER_ARGB_8888);
Glide.with(getApplicationContext())
.load(yourUri)
.asBitmap()
.override(50,50)// Example
.videoDecoder(fileDescriptorBitmapDecoder)
.into(yourImageView);
but when i used above code i get "cannot access from outside of package" error for VideoBitmapDecoder.
exmaple link = "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"
any idea?
thanks
I've used this snippet to get thumb nail out of video frames. Try it
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(media_url,
MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable BD = new BitmapDrawable(thumbnail);
videoView.setBackgroundDrawable(BD);
I am using this lib:
https://github.com/nostra13/Android-Universal-Image-Loader
I want to load an image thumbnail into an ImageView using the lib.
What content uri can I pass into:
imageLoader.displayImage(imageUri, imageView); //?
The docs give an example of using a content url:
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
I pretty much want to switch from doing this:
return MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
to using the lib.
As you can see I have the id but I need to construct a url for MICRO_KIND image thumbnails...
I think you can use next URI format: content://media/external/images/thumbnails/13
String uri = "content://media/external/images/thumbnails/" + id;