I am very new to Android development!
I am working on a camera-related app, and I encountered a problem that the pictures taken are not immediately displayed in 'Photos' app in Android 5.0.1, but when I restart the phone, and take a picture one more time, the previous pictures would all be displayed in 'Photos' app. And this problem only occurs in Android 5.0.1. I tried it on Android 4.3, and there isn't the problem.
And I do add the following codes in my program:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(new File("file://" + Environment.getExternalStorageDirectory() + "/DCIM")); //out is your output file
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
Could anybody help me out there please????
Related
In Android Nougat and above versions the method to capture image from camera intent is changed and following code is working fine for me.
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// File file = new File(AppGlobal.URI_CAPTURED_IMAGE.getPath());
AppGlobal.URI_CAPTURED_IMAGE = FileProvider.getUriForFile(parent, context.getPackageName() + ".provider", AppGlobal.getOutputMediaFile());
}
else
{
AppGlobal.URI_CAPTURED_IMAGE = Uri.fromFile(AppGlobal.getOutputMediaFile());
}
camera.putExtra(MediaStore.EXTRA_OUTPUT, AppGlobal.URI_CAPTURED_IMAGE);
camera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(camera, WritePostFragment.REQUEST_CODE_PHOTO_CAPTURE);
In onActivityResult method I'm able to show the captured image in ImageViews as well.
Glide.with(parent).load(AppGlobal.URI_CAPTURED_IMAGE).centerCrop().into(profilePic_iv);
But when i use the same uri to get File then it says no file exists at given path. What could be the issue? How can i parse the Uri to get File. It seems to be version related stuff.
Sample uri is as follows:
content://com.example.demo.provider/external_files/Camera/IMG_20171213_015646.jpg
How can i parse the Uri to get File
You don't. Moreover, you do not need to. Your file is whatever AppGlobal.getOutputMediaFile() returns. You need to hold onto that value (including putting it in the saved instance state Bundle, in case your process is terminated while the camera app is in the foreground). See this sample app for how to use ACTION_IMAGE_CAPTURE with FileProvider.
My app allows users to take a picture with the camera and saves it to a custom folder. This works fine. The issue being seen by users (Samsung devices) is that the Gallery app crashes when launched. The only way to fix this is to go into Pictures folder on the device and remove the myApp custom directory.
Has anybody seen this issue before? I can paste the image creation/saving code if needed but that is working just fine.
Is this a permissions issue?
Could this be the issue?
You can try update system according to system version:
File mediaStorageDir = new File(getPictureDirectory());
Uri contentUri = Uri.fromFile(mediaStorageDir);
if(Build.VERSION.SDK_INT >= 19){
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
getApplication().sendBroadcast(mediaScanIntent);
}else{
getApplication().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, contentUri));
}
It works for me. Hope it help!
I am developing an Android app for Jellybean, KitKat and Lollipop and several different devices.
At the beginning of project, I used the Jellybean API library with target API 'anroid-18' using 'Samsung galaxy note 2014 edition'.
A few months later, the OS of the device started upgrading to KitKat OS, so I changed the library to 'android-19' in which version number is 4.4.2.
I have new device named Galaxy S tab 2 now.
I have the three versions and two devices now and my default API is 4.4.2.
The problem occurred when capturing pictures using the app:
if (Build.VERSION.SDK_INT >= AlopexBuild.VERSION_SUPPORT.KITKAT) {
Intent mediaScanIntent
= new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.parse("file://"+ path);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(
new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ path)));
}
The following code has no problem in Jellybean:
Intent mediaScanIntent
= new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.parse("file://"+ path);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Why is this happening?
Jellybean OS + Kitkat API = Failed
Kitkat OS + Kitkat API = OK
Lollipop OS + KitKat API = OK
Problematic code is
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ path)));
According to the wiki articles references for each OS when releasing the new KitKat and Lolipop OS and moving from Jellybean they changed to a new kind of runtime enviroment. The code only compatible in this new enviroment, you'll need to try a different method for other enviroments like Jellybean.
See here and here
I am currently developing an Android Application where users take pictures which are stored in External Memory on the device then I am trying to also get the Gallery to scan the file to add the new media to the Gallery however this does not seem to update until the device reboots.
The code I am using is as follows:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Log.v("INFO", mCurrentPhotoPath.toString());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
After taking a picture with my app I can verify it exists using a file manager app on the device and also Logging the file path it appears to be correct before passing it to Media Scanner
file:/storage/emulated/0/Pictures/JPEG_20140712_163043_1418054327.jpg
Thanks Aaron
I managed to solve this. The issue was due to, where I setup the mCurrentPhotoPath. So I updated the code to use
photoFile = createImageFile();
mCurrentPhotoPath = photoFile.getAbsolutePath();
Media Scanner take some to scan and insert show results after calling sendBroadcast you can use MediaScannerConnection method onScanCompleted
and then check image in gallery.
for more reference follow Link
I am currently using a photo gallery I built through GridLayout but this seems to use a lot of memory (OOME coming up) when dealing with a lot of Bitmaps. Therefore, I am trying to use the Intent.ACTION_GET_CONTENT to open photos on the device and select photos through there.
The camera function works correctly as it takes the photo, returns to the onActivityResult function, and updates it to my Gridlayout photo gallery.
I am currently on a Nexus 7 running Android 4.4 (KitKat)
[PROBLEMS]
When I try to view photos through Intent.ACTION_GET_CONTENT, none of the new photos I just took through the application are there.
Running Intent.ACTION_GET_CONTENT to filter results from a specific directory and file type doesn't seem to work.
[Code information]
MainForm is a (class)
photoPath = new File(directory, photoName)
directory is a (File) set to my current working directory
photoName is a (String)
CAMERA_CODE is an (int)
[MY CODE]
Here is my code for launching the camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cUri = Uri.fromFile(photoPath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cUri);
startActivityForResult(intent, CAMERA_CODE); // launch camera
[What I tried + Errors for Updating Photo Gallery library]
MediaScannerConnection
I tried to use this to update the photolist and then launch the intent, but it did not work (I may be using this incorrectly, but it was suggested in this post)
MediaScannerConnection.scanFile(MainForm.this, new String[] { directory.getAbsolutePath() },null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setData(uri);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 12345);
}
});
getContentResolver()
I read in this question to try using this method, but it doesn't seem to work either
MainForm.this.getContentResolver().notifyChange(MediaStore.Images.Media.INTERNAL_CONTENT_URI, null);
sendBroadcast
In this question and here to re-index photos on the device, the answer suggested using sendBroadcast
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(directory)));
but I was greeted with this error
E/AndroidRuntime(30177): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=30177, uid=10122
What does this error mean?
[What I tried + Errors for ACTION_GET_CONTENT intent]
No matter what I put under intent.setData, opening the intent doesn't seem to open that folder, but instead it opens the android browser under the tab "Recent"
I followed this question
ANY TIPS OR SUGGESTIONS WOULD BE GREATLY APPRECIATED! If there are any other codes or log results I should post, please comment below!