I have a problem when I'm trying to get video thumbnail from phone gallery videos. I have a method which is getting bitmap of video thumbnail,but this method is not working perfectly. The minus of mine method is that,for some videos it's not working and I'm getting "FileNotFoundException",but for some videos this method is working fine.
private void pickVideosFromGallery() {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_VIDEO);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK){
return;
}
switch (requestCode)
{
case SELECTED_VIDEO:
Uri uri = data.getData();
String[] projection = {MediaStore.Video.VideoColumns.DATA};
Cursor cursor = getContext().getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bit = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
break;
}}
07-16 20:11:49.647 32387-32387/com.example.tstv.keepfavoritevideos E/folderFragment: Video Path: android.graphics.Bitmap#71d9de1
07-16 20:12:49.992 32387-32387/com.example.tstv.keepfavoritevideos E/MediaMetadataRetriever: setDataSource - FileNotFoundException
07-16 20:12:49.992 32387-32387/com.example.tstv.keepfavoritevideos E/folderFragment: Video Path: null
As you can see from that LOG, for some videos its working, and I am getting bitmap,but for some its not working. WHY?
Related
So I used this to open my image choose upon button click
//Open image chooser
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
and used onActivityResult
private final static int SELECT_PHOTO = 12345;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("CALLED", "OnActivity Result");
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
Log.e("img", "It worked");
// Do something with the bitmap
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}
In logcat, OnactivityResult is not being called, and I cant figure out why. So when I click the button, the image chooser pops up, I choose an image, and then it exits back to the main screen.
Am I missing something, as I've followed others' code but I still get the same thing
Give this a try:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(photoPickerIntent,"Select:"), SELECT_PHOTO);
The Code Works perfectly but Im having problem with the Permission Denied but i already put READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission on Android Manifest any idea. Already Tried this one Unable to decode stream: java.io.FileNotFoundException (Permission denied)
BitmapFactory﹕ Unable to decode stream - null
E/BitmapFactory: Unable to decode stream:
java.io.FileNotFoundException:
/storage/emulated/0/DCIM/Camera/IMG_20201001_125759.jpg (Permission
denied)
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
MainActivity
//Open phone gallery
private void getImageFromGallery(){
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Check if the intent was to pick image, was successful and an image was picked
if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.the_grid_image_preview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
If you're using Android version 6.0 or up, you need to ask for permissions in runtime:
https://developer.android.com/training/permissions/requesting
For this I use a nice wrapper called Dexter which simplifies it and is really easy to use.
You can then write this function in your Fragment:
private fun checkFilePermissions(onPermissionGranted: () -> Unit) {
Dexter.withActivity(activity)
.withPermissions(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
)
.withListener(object : BaseMultiplePermissionsListener() {
override fun onPermissionsChecked(response: MultiplePermissionsReport) {
if (response.areAllPermissionsGranted()) {
onPermissionGranted()
}
}
})
.check()
}
And in when you call your method getImageFromGallery() just invoke it like this:
...
checkFilePermissions {
getImageFromGallery()
}
Following this solution I have run into an issue with the code presented. The app forces a close every time I pick a contact from the list that pops up. Something tells me the culprit has something to do with this which I found in logcat.
Here is my version of the code:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NUMBER));
name1.setText(name);
num1.setText(number);
}
break;
}
}
Take a look at the logcat
Good day,
The following code works when I select an image from the Google Photo app. However, when I select the same image from Samsung stock gallery, my app crashes.
I did some troubleshooting and realize the problem is with the getContentResolver().query():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
//Error happens when program try to run the following line of code
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_LONG).show();
}
}
Please help me. Thank you.
I am using the Below Code for get the image and video from gallery, but can't Detect the photo or video selection from library
Intent intent = new Intent();
intent.setType("video/*,image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_VIDEO);
Did you used onActivityResult to get the selected image ? code will be like below.
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex); // file path of selected image
cursor.close();
// Convert file path into bitmap image using below line.
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
// put bitmapimage in your imageview
yourimgView.setImageBitmap(yourSelectedImage);
}
}
}
For intent you can try with this.
final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);