Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(localUrl,
MediaStore.Video.Thumbnails.MINI_KIND);
这里位图给我空值。
答案 0 :(得分:0)
我遇到的完全相同的情况,只是它无法在更高版本或高额定设备中创建视频缩略图。您正在使用OnActivityResult获取视频路径。
if (requestCode == 3 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
String mimeType = getActivity().getContentResolver().getType(uri);
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
Log.e("PICTURE PATH", picturePath);
File file = new File(picturePath);
long length = file.length();
length = length / 1024;
long length2 = length / 1024;
if (length2 > 25) {
Toast.makeText(getActivity(), "You cannot upload file more than 25 MB", Toast.LENGTH_SHORT).show();
} else {
if (mimeType.contains("image")) {
Log.e("mimeType", mimeType);
Log.e("gif", picturePath);
Intent intent = new Intent(getActivity(), UploadImagePostActivity.class);
intent.putExtra("DESTINATION", picturePath);
startActivity(intent);
getActivity().finish();
} else if (mimeType.contains("video")) {
Log.e("mimeType", mimeType);
Log.e("Video path", picturePath);
Intent intent = new Intent(getActivity(), UploadVideoPostActivity.class);
intent.putExtra("VIDEO_DESTINATION", picturePath);
startActivity(intent);
getActivity().finish();
}
}
}
现在您将获得视频路径,并且您必须在获取视频路径后制作视频缩略图。有时它可能会引起无法获得正确路径的问题,因为某些设备具有不同的路径层次结构。因此,您必须将缩略图保存在特定位置并将其检索回来。代码如下。
// type 2 for video
File uri = new File(filePath);
//create thumbnail image
thumb = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
img_tumbnail.setImageBitmap(thumb);
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
thumbNailfile = savebitmap(thumb);
Log.e("Thumbnail path", "" + thumbNailfile);
} catch (IOException e) {
e.printStackTrace();
}
}
});
和特定路径中的保存缩略图文件。现在它将返回正确的文件路径。
// save bitmap into internal memory
public static File savebitmap(Bitmap bmp) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp = ((BitmapDrawable) img_tumbnail.getDrawable()).getBitmap();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//
thumnailImagePath = Environment.getExternalStorageDirectory() + File.separator + "123.jpg";
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "123.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f;
}