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?

Community
  • 1
  • 1
AlexAndro
  • 1,918
  • 2
  • 27
  • 53
  • 2
    define not working ... any logcat logs ? – Selvin Dec 06 '12 at 12:14
  • so bmThumbnail is null after extract ? try to call extract in AsyncTask ... – Selvin Dec 06 '12 at 12:30
  • @AlexAndro Please check your internet connection. – Dipak Keshariya Dec 06 '12 at 12:41
  • 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 ... – Selvin Dec 06 '12 at 12:56
  • @Selvin 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. – AlexAndro Dec 06 '12 at 13:14
  • 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 ... – Selvin Dec 06 '12 at 13:17
  • @Selvin 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? – AlexAndro Dec 06 '12 at 13:47
  • hmmm i think that even on 2.3 it loads whole video to some temporary place before it generates thumb ... – Selvin Dec 06 '12 at 13:50
  • On Android 5 Dose not Working Too , What Should To Do ? – Criss Aug 23 '15 at 10:53
  • Possible duplicate of [Is it possible to Generate a thumbnail from a video url in android](https://stackoverflow.com/questions/22954894/is-it-possible-to-generate-a-thumbnail-from-a-video-url-in-android) – Mehdi Dehghani Mar 28 '19 at 05:30

3 Answers3

2

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

Kaushik
  • 6,150
  • 5
  • 39
  • 54
William Seemann
  • 3,440
  • 10
  • 44
  • 78
  • 1
    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. – AlexAndro May 12 '14 at 13:49
  • Too laggy I am still looking for a feasible method to get the thumbnails of a local file the file path or uri – Lion789 Aug 01 '15 at 20:38
  • the size of this library is almost 25MB! – Amir Hossein Ghasemi Jan 17 '17 at 10:56
  • @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. – William Seemann Jan 17 '17 at 16:45
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.

Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • 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+. – AlexAndro Mar 06 '13 at 23:07
  • Here is a short piece of my work, where I have used Amazon API to access the videos: http://stackoverflow.com/questions/13287233/how-to-play-a-video-from-amazon-s3-in-android-app – AlexAndro Mar 06 '13 at 23:09
  • @AlexAndro 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. – Warpzit Mar 07 '13 at 02:43
  • @AlexAndro 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. – Warpzit Mar 07 '13 at 02:43
  • 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...) – AlexAndro Apr 01 '13 at 19:03
  • @AlexAndro it was never intended to work with a stream. You may call it a feature bug. – Warpzit Apr 02 '13 at 06:20
  • 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? – Lion789 Aug 01 '15 at 20:38
  • @Lion789 your server needs to have the original vidieo in order to generate a thumbnail. – Warpzit Aug 03 '15 at 06:21
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);
eleven
  • 6,779
  • 2
  • 32
  • 52