0
如您所知,android上的內置媒體掃描儀會定期掃描整個文件系統並生成媒體(照片+視頻)預覽縮略圖。這些是Gallery應用程序正在顯示的縮略圖。獲取MediaScanner自動生成的視頻縮略圖
在我的應用我有一個旋轉木馬,我想顯示自動生成縮略圖視頻僅。出於速度的原因,因爲他們已經在那裏。不幸的是,這些文檔對於如何實際獲取它們非常缺乏。
public class MainActivity extends Activity {
private Cursor cursor;
private int columnIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Video.Thumbnails.VIDEO_ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
System.err.println("qqq video cnt=" + cursor.getCount());
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(this));
// Set up a click listener
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Get the data location of the image
String[] projection = {MediaStore.Video.Media.DATA};
cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
cursor.moveToPosition(position);
// Get image filename
String imagePath = cursor.getString(columnIndex);
// Use this path to do further processing, i.e. full screen display
System.err.println("qqq path=" + imagePath);
}
});
}
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
System.err.println("qqq count " + cursor.getCount());
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
Uri uri = Uri.withAppendedPath(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
System.err.println("qqq getview uri="+uri.toString());
// Set the content of the image based on the provided URI
picturesView.setImageURI(uri);
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
//picturesView.setImageBitmap(BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_btn_speak_now));
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}
我得到的日誌如下。它確實顯示了正確數量的視頻(2),但即使縮略圖媒體URL「看起來不錯」,返回的Bitmap對象也是零。有任何想法嗎?
W/System.err: qqq video cnt=2
W/System.err: qqq count 2
W/System.err: qqq count 2
W/System.err: qqq count 2
W/System.err: qqq getview uri=content://media/external/video/thumbnails/2484
W/System.err: qqq getview uri=content://media/external/video/thumbnails/2483
W/System.err: qqq count 2
W/System.err: qqq getview uri=content://media/external/video/thumbnails/2484
W/System.err: qqq count 2