Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(localUrl,
MediaStore.Video.Thumbnails.MINI_KIND);
Here bitmap gives me null value.
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(localUrl,
MediaStore.Video.Thumbnails.MINI_KIND);
Here bitmap gives me null value.
The exact same condition I have faced, Only it was unable to create video thumbnail in higher version or high rated device. You are getting path of video in OnActivityResult something like this.
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();
}
}
}
Now you will get the path of video and you have to make video thumbnail after getting video path. sometime it may raise the problem of not getting correct path because some devices have different path hierarchy. so you have to save your thumbnail at a particular position and retrieve it back. code is give below.
// 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();
}
}
});
and the saving thumbnail file at specific path. now it will return the correct path of file.
// 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;
}