After creating video and save it, I'm trying to make it visible in the gallery app by scanning the video path using the code below :
Path of saved video :
public String getPath() {
String videoFileName = System.currentTimeMillis() + ".mp4";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/MyFolderApp");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File videoFile = new File(storageDir, videoFileName);
savedVideoPath = videoFile.getAbsolutePath();
}
return savedVideoPath;
}
Scan path :
private void galleryScanner(String path) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
After saving the video, I call the scan method like this: galleryAddPic(getPath());
Weird behavior : If I open the Google Photos app and go to Album > MyFolderApp, I could see the saved video there, also when I exit the Google Photos app and return to Gallery System, the saved video shown normally
Related
I would like, as title suggests to find an image via Android File Explorer. I'm taking a photo and saving in on external storage but that part of storage is only available to app itself.
Basically, before I start Camera activity I have following code:
public File getPhotoFile(Contact contact, Context context) {
File externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (externalFilesDir == null) {
return null;
}
File f = new File(externalFilesDir, contact.getPhotoFilename());
return f;
}
Method in Contact class getPhotoFilename():
public String getPhotoFilename() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return "IMG_" + this.getId() + "_" + timeStamp + ".jpg";
}
And in activity:
cameraButton.setOnClickListener((View v) -> {
File f = getPhotoFile(newContact, this);
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.email.fileprovider",f);
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(captureImage, REQUEST_TAKE_PHOTO);
});
I display full path, like this:
Log.i("PATH", filePath); //file path is member variable which just holds f.getAbsoluthPath();
What I get in output is:
/storage/emulated/0/Android/data/com.example.email/files/Pictures/IMG_0_20200428_001544.jpg
I've tried find that image in File Device Explorer, but no luck.
In my app I have created a option for taking Photo and Viewing taken Photos.
For taking Photos I am using default camera of mobile.
I am using below code for passing intent
public void intentForTakePicture() {
Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (picIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
Log.e(LOG_TAG,"Exception while trying to create image file!!");
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.app.photo.fileprovider",
photoFile);
picIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(picIntent, TAKE_PHOTO);
Log.d(LOG_TAG,"request code is: "+TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir("Pictures/photo/");
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
Log.d(LOG_TAG, "currentPhotoPath: " + currentPhotoPath);
return image;
}
In onActivityResult I am setting imageview whatever photos comes there
But the issue is, I am able to take photos but taken Photo is not getting back in onActivityResult.
Note: Getting this issue only for ASUS_X01BDA mobile.
I also checked the mobile camera permissions for my app. Got camera permission for my app.
I doesn't know the problem.
Please Anybody help me to find the cause and to solve it
I've found ways that have supposedly worked for people to retrieve the uri/bitmap of the camera taken image.
But when I try and create a bitmap out of the non-null uri that I'm getting the file isn't created and doesn't exist.
this the the function which I use to open the camera intent(Which is copied from stackoverflow):
private void openBackCamera() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
currentImageBase64 = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(currentImageBase64);
Log.d("2","file 1: " +file.exists());
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext(),"com.mydomain.fileprovider",file);
currentImageBase64 =outputFileUri+"";
Log.d("2",outputFileUri+" is the uri got from camera");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
and this is how I handel my images:
Log.d("2","camera request!"+ currentImageBase64);
File imgFile = new File(currentImageBase64);
if(imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivPreview.setImageBitmap(myBitmap);
}else{
Log.d("2","file does not exist");
}
note that the camera request log shows that currentImagesBase64 is not null and has a value "content://com.mydomain.fileprovider/name/Pictures/20190121_173332.jpg"
the file anyway, does not exist.
/**
edit: the problem was insufficent permissions, camera and read&write permissions were requested and the problem was resolved!
**/
Turn out Permissions were the problem, once I gave runtime camera read&write storage permissions the problem was fixed!
I hope someone can help me with this problem.
I have an app which enables the user to take a video (.mp4) or pick existing one. If taking a video, the video is saved to a public directory and I triggered the Android to scan the file to add it to media gallery.
Problem is, taking the video is working fine, I can preview the finished video just fine in my app. But this same video won't appear in media gallery and won't be accessible by other apps -except the FileManager-, even though other .mp4 in the same folder appear in the list just fine.
Further info:
In FileManager app, The not-appearing files have icon video icon while the appearing ones got a thumbnail. I can trigger these not-appearing files to be added to media gallery apps by cut and paste the files in FileManager app (so I believe is not due to files being corrupted).
The scan code works fine for the my code that take images from existing camera app, it just won't work for the video ones...
Is there any need for additional permission for this to work? I've added/asked/request permission for write and read from ext. storage and camera in my manifest and code.
This below is how I take the Video and scan it into gallery :
private void takeVideo() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(ctx.getPackageManager()) != null) {
// Create the file where photo should go
File mediaFile = null;
try {
mediaFile = getOutputMediaFile(ctx, MEDIA_TYPE_VIDEO);
} catch (IOException ex) {
Log.e("FragCamera", "takeVideo() : Error occurred while creating File." + ex);
}
if (mediaFile != null) {
Uri mediaUri = Uri.fromFile(mediaFile);
Log.d("FragCamera", "takeVideo() mediaUri: " + mediaUri);
currMediaUri = mediaUri;
currPhotoPath = mediaFile.getAbsolutePath();
Log.d("FragCamera", "takeVideo() currPhotoPath: " + currPhotoPath);
//make the new file available for other apps
updateMediaGallery(mediaFile);
MediaScannerConnection.scanFile(
ctx,
new String[]{currPhotoPath},
new String[]{"video/mp4"},
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.v("FragCameraScan",
"file " + path + " was scanned seccessfully: " + uri);
}
});
takeVideoIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mediaUri);
this.startActivityForResult(takeVideoIntent, I_REQUEST_VIDEO_CAPTURE);
}
}
}
private void galleryAddPic(String filePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(filePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
ctx.sendBroadcast(mediaScanIntent);
}
Logcat value for each Log in the code above :
D/FragCamera: takeVideo() mediaUri: file:///storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
D/FragCamera: takeVideo() currPhotoPath: /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
V/FragCameraScan: file /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4 was scanned seccessfully: null
try using this function
public Uri addVideo(File videoFile) {
ContentValues values = new ContentValues(3);
values.put(MediaStore.Video.Media.TITLE, "My video title");
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
return getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
the 'values' is simply meta data about the video
Adding file path extra to image capture intent causes camera app to malfunction on TF300t Android tablet with stock system version 4.2.1. Pressing "done" button does nothing - not even closing camera app activity. No result is returned.
The code I'm using was extracted from Adroid developers site
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imageFile = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
startActivityForResult(cameraIntent, THIS_CAMERA_REQUEST);
With createImageFile() defined as:
private File createImageFile() throws IOException {
File outputDir = getBaseContext().getCacheDir();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "photo_" + timeStamp + "_";
File image = new File(outputDir, imageFileName);
return image;
}
When line
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
is removed, camera app acts as expected.
Is there any resonable workaround ? I would rather not build a camera app myself just to take a photo.
Problematic line:
File outputDir = getBaseContext().getCacheDir();
I've replaced it with:
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "1mind_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(), imageFileName);
return photo;
}
Turns out, image has to be stored on external storage not in cache dir.