3

我正在尝试创建一个包含视频详细信息的 ListView,包括视频缩略图。

在适配器的 getView 方法中我调用

new ThumbnailCreatorTask(videoThumbnail).execute(path) for every row

这是 ThumbnailCreatorTask:

private class ThumbnailCreatorTask extends AsyncTask<String, Void, Bitmap>{

    private final WeakReference<ImageView> imageViewReference;

    public ThumbnailCreatorTask(ImageView imageView){
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        return ThumbnailUtils.createVideoThumbnail(params[0], Thumbnails.MICRO_KIND);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {

        if (imageViewReference != null && bitmap != null) {
            ImageView imageView = imageViewReference.get();
            imageView.setImageBitmap(bitmap);
        }
    }
}

创建一些行后,我遇到以下崩溃:

07-16 16:26:30.060: E/dalvikvm-heap(17318): 1843200-byte external allocation too large for this process.
07-16 16:26:30.060: E/GraphicsJNI(17318): VM won't let us allocate 1843200 bytes
07-16 16:26:30.190: E/ThumbnailUtils(17318): Got OOM error
07-16 16:26:30.190: E/ThumbnailUtils(17318): java.lang.OutOfMemoryError: bitmap size exceeds VM  budget
07-16 16:26:30.190: E/ThumbnailUtils(17318):    at     android.graphics.Bitmap.nativeCreate(Native Method)
07-16 16:26:30.190: E/ThumbnailUtils(17318):    at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
07-16 16:26:30.190: E/ThumbnailUtils(17318):    at android.graphics.Bitmap.createBitmap(Bitmap.java:432)
07-16 16:26:30.190: E/ThumbnailUtils(17318):    at android.media.MediaMetadataRetriever._getFrameAtTime(Native Method)
07-16 16:26:30.190: E/ThumbnailUtils(17318):    at android.media.MediaMetadataRetriever.getFrameAtTime(MediaMetadataRetriever.java:201)
07-16 16:26:30.190: E/ThumbnailUtils(17318):    at android.media.MediaMetadataRetriever.getFrameAtTime(MediaMetadataRetriever.java:225)
07-16 16:26:30.190: E/ThumbnailUtils(17318):    at android.media.ThumbnailUtils.createVideoThumbnail(ThumbnailUtils.java:155)

正如我认为我的方法是完全错误的。在我的情况下应该如何处理缩略图创建?

4

1 回答 1

0

我遇到了同样的问题,这是我解决它的方法:

        // the thumbnail files will be generated in the internal data folders then deleted after upload
        String internalPath = context.getFilesDir().getAbsolutePath();
        // now we need the file name
        String thumbnailFileName = internalPath + "/" + file.getName() + sizeName + THUMBNAIL_EXT;

        ContentResolver crThumb = context.getContentResolver();
        String[] proj = { BaseColumns._ID, MediaColumns.DISPLAY_NAME, MediaColumns.DATA };
        Cursor cursor =
                crThumb.query(
                        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        proj,
                        MediaColumns.DISPLAY_NAME + "=?",
                        new String[] { file.getName() },
                        null);
        cursor.moveToFirst();
        long fileid = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        Bitmap imageBitmap =
                MediaStore.Video.Thumbnails.getThumbnail(crThumb, fileid, MediaStore.Video.Thumbnails.MICRO_KIND, options);
        //resize
        imageBitmap = Bitmap.createScaledBitmap(imageBitmap, sizeWidth, sizeHeight, false);

        // output
        ByteArrayOutputStream baos = new ByteArrayOutputStream(BYTEARRAYOUTPUTSTREAM_BUFFER);

        // format to jpeg quality set to 70
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 70, baos);
        byte[] imageData = baos.toByteArray();

        FileOutputStream fos = new FileOutputStream(thumbnailFileName);
        fos.write(imageData);
        fos.close();
于 2013-07-16T14:47:58.093 回答