简体   繁体   中英

thumbnail video from url using image?

I already success display thumbnail video from URL on my android app when internet connection is connected, but when internet connection is off the thumbnail doesn't display.

here is my code.

Bitmap bmThumbnail;

bmThumbnail = ThumbnailUtils.createVideoThumbnail("http://somedomain.com/video/myvideo.mp4", Thumbnails.MICRO_KIND );
imgPhoto.setImageBitmap(bmThumbnail);

i want the thumbnail still display although connection is off,there is away to achieve like save the cache on sdcard first, like image cache does? or any other solution to show thumbnail video when internet connection is off? thanks,

public static String getBitmapFromURL(final Activity activity, String link,
            String filename) throws FileNotFoundException,
            MalformedURLException, IOException {

        /*--- this method downloads an Image from the given URL, 
         *  then decodes and returns a Bitmap object
         ---*/

        File file = null;


        file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath()
                + CommonVariable.KCS_IMAGE_FOLDER_NAME_PHONE_MEMORY);

        // have the object build the directory structure, if needed.
        if (!file.exists()) {
            file.mkdirs();
        }
        // create a File object for the output file
        File outputFile = new File(file, filename);
        FileOutputStream fos = new FileOutputStream(outputFile);
        BufferedOutputStream out = new BufferedOutputStream(fos, 1024);
        URL url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();

        out = new BufferedOutputStream(fos, 1024);

        int b;
        while ((b = input.read()) != -1) {
            out.write(b);
        }
        out.close();
        connection.disconnect();

        return outputFile.getAbsolutePath();

    }

use this function,it will return string of sdcard path.and use this path u can set bitmap image using below function:

    public static void setImagesNew(ImageView img, String pathName,
                Activity activity) {


                  Bitmap  bmThumbnail = ThumbnailUtils.createVideoThumbnail(pathName, Thumbnails.MICRO_KIND );
img.setImageBitmap(bmThumbnail);


            bmp = null;
            System.gc();
            Runtime.getRuntime().gc();

        }

i hope this is useful to you...............

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM