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.
Related
Currently I have the following code that allows a user to choose an image.
int requestCode = 1337;
Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
chooserIntent.setType("image/*");
chooserIntent = Intent.createChooser(chooserIntent, "Please choose a picture");
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(chooserIntent, requestCode);
My question is:
does Android guarantee that the returned Uri is always pointing to a location on disk, or is it possible that it might be pointing to somewhere on the internet too?
P.S. although I am not sure about this, the Uri returned by this piece of code seems to always start with content:// - I am not sure whether or not this holds for all possible return values, I thought I would just add this here to help out any possible question answerers.
does Android guarantee that the returned Uri is always pointing to a
location on disk, or is it possible that it might be pointing to
somewhere on the internet too?
It is possible to have Uri other than local disk i.e. it can be remotely as well. You will get URL from remote then convert it to Uri and use it.
From official docs:
An ACTION_GET_CONTENT could allow the user to create the data as it
runs (for example taking a picture or recording a sound), let them
browse over the web and download the desired data, etc.
Convert Url to a Uri (Reference):
Uri uri = Uri.parse( "http://www.facebook.com" );
As android is increasing its security and changing some stuff now a days to make Android OS more secure to Vulnerabilities, its a good thing to understand new structure for developers to get into it.
I have been away from development from last couple of years.I just noticed that some stuff have been changed e.g file scheme of URI.
Case 1 : :
I am launching camera intent with custom URI mentioned below.
var takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(packageManager) != null) {
// Create the File where the photo should go
var photoFile: File? = null
try {
photoFile = createImageFile()
} catch (ex: IOException) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
val photoURI: Uri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".fileprovider",
photoFile)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
}
}
photoURI is custom URI.
When i took picture and check URI it has file:// in start from where i can get file path and i can do some stuff with it(i am uploading it to server)
Case 2
I am launching simple intent to pick image from gallery
val intent = Intent()
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), TYPE_IMAGE)
and now when i pick image and check URI its has content:// in start. And the trouble starts when i try to get file path from it. i have been searching into SOF but none of solutions worked for me.
My main purpose it get file path is that i want to pick file from device and upload it to server.But with content:// scheme i can not make a file from URI and thus can not send this file server.
i just read CommonsWareBlog about scheme system. it cleared few things but i still don't know how can i switch from content:// too file:// in my onActivityResult!
i would like to mention it again that current answers on SOF are not working for me that's why i have to ask this question in details.
I am using 7.0 device emulator
This is just an example of my problem in actual i am picking up PDF and DOCX files and want to upload them on server and after picking files i have URI with content:// scheme and i can not make a file and send to server.
i am not sure what i am missing about schemes.
I hope this question will help many other developers which are new to file system.
pleasure try to explain by code example according to my problem of picking file from intent (if you like)
Since Android 7.0, returning file:// scheme is discouraged (android changelog), since it can leak files from private folders. You can still encounter it, especially when using apps that target below Nougat.
Short answer is when you query for Intent.ACTION_GET_CONTENT You receive content:// uri that you can ContentResolver.openInputStream(Uri) on to obtain the data. This ensures item you picked is read-only, and masks it's real source - it might've been a file, provided remotely or generated by provider all together.
This is my implicit intent to invoke image editing apps on the device:
startActivity(new Intent(Intent.ACTION_EDIT).setDataAndType(myUri,
getMimeType(myUri)).setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
And this is how I getMimeType:
public String getMimeType(Uri uri) {
String mimeType = null;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
For some apps it crashes to load:
On the app Sketch_Camera only an invisible page loads up and disables interaction with my app.
On the app AirBrush it loads the app but crashes with this message "Failed to load image".
Is it related to minimum sdk version as mine is 16?
I've tested this on minimum sdk version of 9 too and no change in result.
Is there anything else that I should add to this intent to work with all the apps?
I've tried putExtra too and it doesn't help:
.putExtra(Intent.ACTION_EDIT, myUri);
I've some gallery apps on my device and all of them launch Sketch_Camera and AirBrush without any problem.
What's happening here? I'm so confused after two days of struggling with this phenomena.
It's a file created from path of one media store file by querying MediaStore.Images.Media.EXTERNAL_CONTENT_URI
There is no guarantee that the other app has rights to this location, or even that your app has rights to this location. For example, the image could be on removable storage. Besides, the file Uri scheme is being banned for cross-app usage, anyway.
Use a content Uri instead. For example, in this sample app, I query MediaStore for videos. Given a Cursor named row positioned at a particular video, I generate the Uri for it via:
videoUri=
ContentUris.withAppendedId(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, row.getInt(row.getColumnIndex(MediaStore.Video.Media._ID)));
This Uri both works for my own purposes (e.g., hand to Picasso to get a thumbnail, hand to VideoView for playback) and for handing to third-party apps (e.g., ACTION_VIEW for playback).
Other than changing the base Uri to the one you queried against (MediaStore.Images.Media.EXTERNAL_CONTENT_URI), the same basic code should work for you.
Also, get rid of the flags from your Intent. Those are only for where the Intent points to your own ContentProvider, which is not the case in either your original code or with the Uri that you create from withAppendedId().
When I launch a Photo Capture intent, the photo path that is gave to me in return is : content://media/external/images/media/40209 but when I look in my device, the photo path should have been something like [..]/pictures/1456164469539.jpg
Do you know how to get the second path from the first ?
note I use the method described there Android ACTION_IMAGE_CAPTURE Intent by yanokwa.
Thanks,
-------------------- EDIT
I launch my intent like so :
private void launchPhotoIntent() {
Uri photoUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
Log.i("renaud","photoUri : "+mPhotoUri.toString());
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(AppConstants.SP,Context.MODE_PRIVATE);
sharedPreferences.edit().putString("test",photoUri.toString()).commit();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
getActivity().startActivityForResult(intent, ACTION_TAKE_PHOTO);
}
and In my Callback :
else if(requestCode == PostMessageWindowFragment.ACTION_TAKE_PHOTO && resultCode == Activity.RESULT_OK){
Uri uri = Uri.parse(sharedPreferences.getString("test",null)); // data.getData();
File file = new File(event.uri.getPath());
Log.i("PICTEST",""+file.length());
}
It logs "0"
When I launch a Photo Capture intent, the photo path that is gave to me in return is : content://media/external/images/media/40209
That is not a path. That is a Uri, pointing to content.
the photo path should have been something like [..]/pictures/1456164469539.jpg
Not necessarily.
First, there are thousands of Android device models and thousands of camera apps (both pre-installed and installed by users), some of which implement ACTION_IMAGE_CAPTURE. What one camera app does will not necessarily match what another camera app does.
Second, if you read the documentation for ACTION_IMAGE_CAPTURE, you will notice that there is no "photo path that is gave to me in return". If you supply EXTRA_OUTPUT, your photo should be in that location. If you do not, use getExtra("data") on the Intent passed to onActivityResult() to get a thumbnail bitmap. You appear to be assuming that the Intent will have a Uri, and few camera apps do that.
Do you know how to get the second path from the first ?
That is not possible in general, as a Uri does not have to point to a file, let alone a file that you can access. Use ContentResovler to work with Uri values, such as openInputStream() to read in the content pointed to by a Uri.
The path you get is the file URI. Try with converting it to path using this Convert file: Uri to File in Android
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