以下のコードを使用してみてください。あなたに役立つかもしれません:
new AsyncTask<Void, Integer, Void>() {
protected void onPreExecute() {
};
@Override
protected Void doInBackground(Void... arg0) {
imageAdapter.images.clear();
initializeVideoAndImage();
return null;
}
protected void onProgressUpdate(Integer... integers) {
imageAdapter.notifyDataSetChanged();
}
public void initializeVideoAndImage() {
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Thumbnails._ID };
String orderBy = MediaStore.Images.Media._ID;
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
final String[] videocolumns = { MediaStore.Video.Thumbnails._ID, MediaStore.Video.Media.DATA };
orderBy = MediaStore.Video.Media._ID;
Cursor videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videocolumns, null, null, orderBy);
int i = 0;
int image_column_index = 0;
if (imageCursor != null) {
image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
int count = imageCursor.getCount();
for (i = 0; i < count; i++) {
imageCursor.moveToPosition(i);
int id = imageCursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
imageAdapter.images.add(imageItem);
}
}
if (videoCursor != null) {
image_column_index = videoCursor.getColumnIndex(MediaStore.Video.Media._ID);
int count = videoCursor.getCount();
for (i = 0; i < count; i++) {
videoCursor.moveToPosition(i);
int id = videoCursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
imageAdapter.images.add(imageItem);
}
}
publishProgress(i);
if (imageCursor != null) {
image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
int count = imageCursor.getCount();
for (i = 0; i < count; i++) {
imageCursor.moveToPosition(i);
int id = imageCursor.getInt(image_column_index);
int dataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
ImageItem imageItem = imageAdapter.images.get(i);
Bitmap img = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
int column_index = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
imageItem.imagePath = imageCursor.getString(column_index);
imageItem.videoPath = "";
try {
File imageFile = new File(Environment.getExternalStorageDirectory(), "image" + i);
imageFile.createNewFile();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (bos != null && img != null) {
img.compress(Bitmap.CompressFormat.PNG, 100, bos);
}
byte[] bitmapData = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(bitmapData);
fos.close();
imageItem.thumbNailPath = imageFile.getAbsolutePath();
try {
boolean cancelled = isCancelled();
// if async task is not cancelled, update the
// progress
if (!cancelled) {
publishProgress(i);
SystemClock.sleep(100);
}
} catch (Exception e) {
Log.e("Error", e.toString());
}
// publishProgress();
} catch (IOException e) {
e.printStackTrace();
}
// imageAdapter.images.add(imageItem);
}
}
imageCursor.close();
if (videoCursor != null) {
image_column_index = videoCursor.getColumnIndex(MediaStore.Video.Media._ID);
int count = videoCursor.getCount() + i;
for (int j = 0; i < count; i++, j++) {
videoCursor.moveToPosition(j);
int id = videoCursor.getInt(image_column_index);
int column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
ImageItem imageItem = imageAdapter.images.get(i);
imageItem.imagePath = videoCursor.getString(column_index);
imageItem.videoPath = imageItem.imagePath;
System.out.println("External : " + imageItem.videoPath);
try {
File imageFile = new File(Environment.getExternalStorageDirectory(), "imageV" + i);
imageFile.createNewFile();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MediaMetadataRetriever mediaVideo = new MediaMetadataRetriever();
mediaVideo.setDataSource(imageItem.videoPath);
Bitmap videoFiles = mediaVideo.getFrameAtTime();
videoFiles = ThumbnailUtils.extractThumbnail(videoFiles, 96, 96);
if (bos != null && videoFiles != null) {
videoFiles.compress(Bitmap.CompressFormat.JPEG, 100, bos);
}
byte[] bitmapData = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(bitmapData);
fos.close();
imageItem.imagePath = imageFile.getAbsolutePath();
imageItem.thumbNailPath = imageFile.getAbsolutePath();
try {
boolean cancelled = isCancelled();
// if async task is not cancelled, update the
// progress
if (!cancelled) {
publishProgress(i);
SystemClock.sleep(100);
}
} catch (Exception e) {
Log.e("Error", e.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
videoCursor.close();
}
protected void onPostExecute(Void result) {
imageAdapter.notifyDataSetChanged();
};
}.execute();
}