It is possible to display a video thumbnail from a URL on Android 4 and above?
Asked Answered
S

3

12

Both of the below works fine on the emulator (2.3.3), but on a real device (Nexus S with 4.1.2) no image is shown for the thumbnail. I will also try to run it on an Android 4 Emulator. If I set a default android:src for the ImageView, it is not shown anymore then. This makes me think that it is replaced, but the ImageView is empty.

public class MainActivity extends Activity {

    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView)findViewById(R.id.img_thumbnail);
        new MyAsync().execute("http://commonsware.com/misc/test.mp4");
    }

    //This version is still not working, but it's more readable (edited: Selvin).
    public class MyAsync extends AsyncTask<String, Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... objectURL) {
            //return ThumbnailUtils.createVideoThumbnail(objectURL[0], Thumbnails.MINI_KIND);
            return ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail(objectURL[0], Thumbnails.MINI_KIND), 100, 100);
        }

        @Override
        protected void onPostExecute(Bitmap result){
             img.setImageBitmap(result);
        }
    }
}

I know that a similar question has been asked before, Displaying video thumbnails in an Android device from a remote video URL, but I have already tried this and same result.

Why doesn't this work on the device and how make it work?

Saylor answered 6/12, 2012 at 12:1 Comment(10)
define not working ... any logcat logs ?Secondguess
so bmThumbnail is null after extract ? try to call extract in AsyncTask ...Secondguess
@Saylor Please check your internet connection.Piano
hmmm starnge ... but it not depends on emulator/device but on android version ... it's working with 2.3.3 but not on 4.1.2 ... i though that it becouse NetworkOnMainTE but even with AsyncTask it's not working ... maybe createVideoThumbnail is not supposed to work with http schema filePath ...Secondguess
@Secondguess Yes you right about SO version, I have tried it with AsyncTask on Android4 and it does not work. I saw that the examples over the internet use sdcard instead of an URL.Saylor
yeap i just look at the OS source ... it check if schema is "file" in other case it return null Bitmap ... so you have to download whole video to local storage(sd or whatever) and then make thumb from this location not from internet ...Secondguess
@Secondguess Hm...Now I see why the ImageView is empty. Anyway I find it strange than an issue that works on 2.3, to not work on 4. I find it absurd to be forced to download the video. This is a test video but imagine to download bigger files....Could you tell me please where did you find those details taht filePath refers to a local file?Saylor
hmmm i think that even on 2.3 it loads whole video to some temporary place before it generates thumb ...Secondguess
On Android 5 Dose not Working Too , What Should To Do ?Ellie
Possible duplicate of Is it possible to Generate a thumbnail from a video url in androidStockish
J
2

Use FFmpegMediaMetadataRetriever to extract a thumbnail at the desired position: FFmpegMediaMetadataRetriever

Jewelfish answered 7/5, 2014 at 16:23 Comment(4)
I have tried your library and it works.Using the sample code from description, Bitmap b = mmr.getFrameAtTime(2000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); works! The byte [] artwork = mmr.getEmbeddedPicture(); returns null anyway.Saylor
Too laggy I am still looking for a feasible method to get the thumbnails of a local file the file path or uriTourneur
the size of this library is almost 25MB!Jenevajeni
@AmirHosseinGhasemi, yes it is. This is due to the fact that the AAR must include individual binaries for each ABI since it uses the NDK. The workaround is to use the offered Gradle flavors or prebuilt AARs and build individual APKs for each architecture.Jewelfish
E
0

This is not possible on Android and Java and as far as I know any other language without downloading the entire video (correct me if I'm wrong). It is with good reason that YouTube and any other large video service provide a simple API for getting a video thumb. So if the server where the videos reside doesn't support this kind of API call your in bad luck.

Etamine answered 6/3, 2013 at 8:24 Comment(8)
Actually my videos reside on Amazon. They provide a such kind of API. With the method described above I have succeeded to generate over 500 thumbnails on an Android 2.3 Emulator in about 2 hours using a recursive thread. That videos ensume more than 32 GB so... I don't know how exactly the emulator works behind the scene but I guess I would observe such a memory occupancy space if it would downloaded it. Maybe it was something temporary I don't know what to say, But it certainly worked on 2.3 and it does not work on Android 4+.Saylor
Here is a short piece of my work, where I have used Amazon API to access the videos: #13287733Saylor
@Saylor well the only thing I've been able to find in the documentation for generating thumbs from videoes is the MediaStore.Video.Thumbnails and it only works for internal and external storage. If your prior solution meet your needs I'd say feel free to use it: download the video 1 at a time and generate thumb and then delete video again :S <-- sounds really stupid to me.Etamine
@Saylor I can only say that the prior solution worked was probably due to a bug, it doesn't follow the documentation at the very least.Etamine
Thanks for the reply but in my opinion, I don't think it was a bug, since exists the class ThumbnailUtils.createVideoThumbnail which the documentation clearly says that it Create a video thumbnail for a video. Of course, does not say anything about storage, yes usually when you see Path you think about local storage. Rather could be a bug on 4.0+ versions since nothing happens.(no warning, no error, no exception, etc...)Saylor
@Saylor it was never intended to work with a stream. You may call it a feature bug.Etamine
Ok I need to find the source of the local video for the thumbnail (the file path or uri) to be able to send it to my servers, how do I do that?Tourneur
@Tourneur your server needs to have the original vidieo in order to generate a thumbnail.Etamine
A
0

Faced the same problem on 2.3 when tried to make thumbnail from file which was located in the cache folder. Setting permission flags solved the problem:

videoFile.setReadable(true, false);
Ax answered 28/11, 2013 at 14:7 Comment(1)
On 2.3 works, the problem is on 4+ for online videos.Saylor

© 2022 - 2024 — McMap. All rights reserved.