Reputation: 4226
I currently have a app where the user click on the floating action button and selects a video file, the file is then saved to a different folder. I want to then show thumbnails of all the video's. I've seen a tutorial series where it is done with MediaStore, but then I can't set the path of the uri.
Can someone please point me in the right direction?
Here is my class to open the gallery and save the video to a different path:
public class Activity extends AppCompatActivity {
private static final int pick = 100;
Uri videoUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openGallery();
}
});
}
private void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, pick);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == pick) {
try
{
Log.e("videopath","videopath");
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File root=new File(Environment.getExternalStorageDirectory(),"MYFOLDER");
if (!root.exists()) {
root.mkdirs();
}
File file;
file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
videoUri = data.getData();
}
}
}
Upvotes: 0
Views: 4226
Reputation: 6114
You can use the ThumbnailUtils
class in Android for this purpose.
public static Bitmap createVideoThumbnail (String filePath, int kind)
this method returns a bitmap of the video.
Second parameter is the kind of bitmap that you need, there are two types:
MediaStore.Images.Thumbnails.MICRO_KIND
which generates thumbnail of size 96 x 96MediaStore.Images.Thumbnails.MINI_KIND
which generates thumbnail of size 512 x 384.Eg:
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);
Here is the API docs for further info[Here].
Upvotes: 8
Reputation: 677
You can easily do it with Glide
:
Glide.with(context).load(videoPath).asBitmap().into(imageView);
Upvotes: 2