使用图片来自网址的缩略图视频?

时间:2013-12-06 03:44:44

标签: java android thumbnails video-capture

当连接互联网连接时,我已成功在我的Android应用程序上显示来自URL的缩略图视频,但是当互联网连接关闭时,缩略图不会显示。

这是我的代码。

Bitmap bmThumbnail;

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

我希望缩略图仍然显示虽然连接已关闭,但首先要保存sdcard上的缓存,就像图像缓存一样?或者在互联网连接关闭时显示缩略图视频的任何其他解决方案? 感谢,

1 个答案:

答案 0 :(得分:0)

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();

    }

使用此函数,它将返回sdcard路径的字符串。并使用此路径,您可以使用以下函数设置位图图像:

    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();

        }

我希望这对你有用...............