how to get video thumb nail from raw folder video on android - android

in my app i need to display video thumb nail which is stored in res/raw folder. i searched regard it. i get the following code.
int id = **"The Video's ID"**
ImageView iv = (ImageView ) convertView.findViewById(R.id.imagePreview);
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv.setImageBitmap(curThumb);
by this code i get video thumb nail from sdcard. but video from res/raw folder it does not show video thumb nail. i tried long time but i could not find solution. i tried in the following way.
I create an array and store the resource id (Ex: int[] videoid={R.raw.vi1,...}) and place the id in MediaStore.Video.Thumbnails.getThumbnail(crThumb, videoid[position], MediaStore.Video.Thumbnails.MICRO_KIND, options);.
please help me. thanks in advance.

You can try to get some frame out of your video just like this:
Uri videoURI = Uri.parse("android.resource://" + getPackageName() +"/"
+R.raw.cashword_video);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(this, videoURI);
Bitmap bitmap = retriever
.getFrameAtTime(100000,MediaMetadataRetriever.OPTION_PREVIOUS_SYNC);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);

Video on res/raw folder of your application aren't managed by Android Media Content Provider, so you cannot obtain their thumbnail from it.
Alternatively, you can try to save them as normal file in your SD and, then, ask to media provider for thumbnail or create it directly via ThumbnailUtils.createVideoThumbnail class (only Android 2.2 or above)

Use Android Glide Library for that
In Project Gradle
repositories {
mavenCentral()
google()
}
In app Gradle
dependencies {
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
}
Use below code in you activity
Glide.with(this).load(R.raw.demo).diskCacheStrategy(NONE).into(imageView);
Glide.with(this).load("file:///android_asset/demo.mp4").diskCacheStrategy(NONE).into(imageView);
Glide.with(this).load("android.resource://" + context.getPackageName() + "/raw/demo").diskCacheStrategy(NONE).into(imageView);

Related

How to make a VideoView Preview UI in Android?

So I'm a beginner in android and I've been trying to experiment media related elements of Android. So far, I was able to display video, but I saw Viber and some other similar apps have some slick way of displaying video like below, wherein when tapped will open an app viewer wherein images/videos of app can be seen there:
Here is my code, it starts video immediately... I tried the .seekTo(100), ..seekTo(0) option but both gives me a black screen.
Uri uri = Uri.parse(mVideoFileName);
mVideoMessageView.setVideoURI(uri);
mediaC.setAnchorView(mVideoMessageView);
mVideoMessageView.start();
I wonder, how they made an overlay of a play button on the video, duration, and even timestamp and have a snapshot of it. Any advice would greatly be appreciated.
Thanks a lot.
int id = **"The Video's ID"**
ImageView iv = (ImageView ) convertView.findViewById(R.id.imagePreview);
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv.setImageBitmap(curThumb);
Also you can use Glide for this
Glide
.with( context )
.load( Uri.fromFile( new File( filePath ) ) )
.into( imageViewGifAsBitmap );

FreeImage problems on Android (NDK)

I tried to use FreeImage library to load PNG as a texture (from memory). That's the fragment of code:
FIMEMORY *fiStream = FreeImage_OpenMemory(streamData, size);
FREE_IMAGE_FORMAT fileFormat = FreeImage_GetFileTypeFromMemory(fiStream, 0);
FIBITMAP *image = FreeImage_LoadFromMemory(fileFormat, fiStream, 0);
int bitsPerPixel = FreeImage_GetBPP(image);
width = (int)FreeImage_GetWidth(image);
height = (int)FreeImage_GetHeight(image);
I'm using FILE with fopen to open file and then read stream to streamData object. File and stream is read correctly.
The result is: fileFormat = -1 and image is NULL.
I also tried to use FreeImage to load PNG file directly from disk using FreeImage_Load, but the result is the same - it returns NULL.
Has anybody faced similar problem? Can you suggest an alternative to FreeImage that can read data from memory?
try this code to load your image from memory:
File file = new File("/sdcard/Images/image1.jpg");
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageBitmap(bitmap);
}

Getting the ImageView image location

I am using the ImageView to display a image with a image stored in SD card. Like this-
ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position];
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
grp_icon.setImageBitmap(bitmap);
And I am trying to get the image location to display the same image in another activity by sending the location in Intent. But it gives a different location Why?
The code where I get the ImageView image location is
String groupName=((TextView) v.findViewById(R.id.grid_item_label)).getText().toString();
ImageView grp_image=(ImageView)v.findViewById(R.id.grid_item_image);
Bitmap b=((BitmapDrawable)grp_image.getDrawable()).getBitmap();
Uri image=getImageUri(getActivity().getApplicationContext(), b);
String path=image.toString();
Intent toMangeGroup=new Intent(getActivity(),ManageGroup.class);
Bundle bundle=new Bundle();
bundle.putString("grp_name", groupName);
bundle.putString("grp_image", path);
toMangeGroup.putExtras(bundle);
startActivity(toMangeGroup);
Here I am getting the path as "content://media/external/images/media/42424" but the actual path of the image is "/sdcard/Letsmeet/letsmeet_media/group_images/1395247992445"
System.out.println("this is path where stored.."
+ Environment.getExternalStorageDirectory()
.getAbsolutePath());
Try this for getting file path on sdcard:
String filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "Letsmeet/letsmeet_media/group_images/" + gridImage[position];
use this code:may help you..
Bitmap mybit=BitmapFactory.decodeFile(file1.getAbsolutePath());
image.setImageBitmap(mybit);
used to store your image in file format and get the image from that path.
Look at this Get filename and path from uri from mediastore. The answer provided by Nikolay Nikiforchuk is the way to go. And that does not only work with the mediafile but will also work good with all the files stored on SDCard
just replace this line
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
with this one
int column_index = cursor.getColumnIndexOrThrow("_data");
That would work fine.

Android get image path from drawable as string

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.
Any ideas how to do that?
These all are ways:
String imageUri = "drawable://" + R.drawable.image;
Other ways I tested
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
String path = path.toString();
String path = otherPath .toString();
based on the some of above replies i improvised it a bit
create this method and call it by passing your resource
Reusable Method
public String getURLForResource (int resourceId) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}
Sample call
getURLForResource(R.drawable.personIcon)
complete example of loading image
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
you can move the function getURLForResource to a Util file and make it static so it can be reused
If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.
InputStream stream = getAssets().open("image.png");
Drawable d = Drawable.createFromStream(stream, null);
I think you cannot get it as String but you can get it as int by get resource id:
int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder
Sample Code
File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
if(!imageFile.exists()){//file doesnot exist in SDCard
imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
}

cover art on android

I am developing a sort of media player for android. The question is how can i get the cover art of audio file on android.
For example the default android media player shows album covers when listing albums, how can i get this artworks.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java method getArtworkQuick.
You can use
MediaMetadataRetriever
class and get track info i.e. Title,Artist,Album,Image
Bitmap GetImage(String filepath) //filepath is path of music file
{
Bitmap image;
MediaMetadataRetriever mData=new MediaMetadataRetriever();
mData.setDataSource(filePath);
try{
byte art[]=mData.getEmbeddedPicture();
image=BitmapFactory.decodeByteArray(art, 0, art.length);
}
catch(Exception e)
{
image=null;
}
return image;
}
I don't know why everybody is making it so complicated you can use Glide to achieve this in simplest and efficient way with just 1 line of code
Declare this path in App Constants -
final public static Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Get Image Uri using Album ID
Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
listOfAlbums.get(position).getAlbumID());
Now simply display album art using uri :-
Glide.with(context).load(uri).placeholder(R.drawable.art_default).error(R.drawable.art_default)
.crossFade().centerCrop().into(holder.albumImage);
Glide will handle caching, scaling and lazy loading of images for you.
Hope it help somebody.
Here i can attach one function that is return album art from media store .
Here in function we just have to pass the album_id which we get from Media store .
public Bitmap getAlbumart(Long album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
Based on your comments to others, it seems like your question is less about Android and more about how to get album art in general. Perhaps this article on retrieving album art from Amazon will be helpful. Once you have a local copy and store it as Nick has suggested, I believe you should be able to retrieve it the way Fudgey suggested.
Android only recognizes files named "AlbumArt.jpg" as Album Covers. Just put the pictures with that name in the album folder and you'll be fine..
I don't know if you read that google is making Stackoverflow the official Android app development Q&A medium but for beginner questions... Now, I know nothing about developing in andriod, but a quick search of the Android Developers site, I found this:
MediaStore.Audio.AlbumColumns
Hopefully it'll help.

Categories

Resources