Camera doesn't work for android 7.0 - android

I'm getting: android.os.FileUriExposedException.
When targeting Android N, file:// URIs are not allowed anymore. I know
We should use content:// URIs instead. However, my app needs file for
both image and video. Any ideas?
mMediaUri = Uri.fromFile(new File(AppHelper.getDirectoryPath(),AppHelper.getFileName() + ".jpeg"));
Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
iCamera.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(iCamera, Constants.INTENT_CALL.CAPTURE_IMAGE);
and onActivityResult
case Constants.INTENT_CALL.CAPTURE_IMAGE:
String filePath=SiliCompressor.with(getActivity()).compress(mMediaUri.toString(), true);
Please add sample code...if Available.

After some research finally got relevant answer to my question just set min target sdk version to 23.

Related

Android 10: ACTION_VIEW becomes broken

One user of my app reports that after upgrading to Android 10 my app on his device stopped displaying an image.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("image/*");
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(scannedUri);
startActivity(intent);
I have "targetSdkVersion 28" so the new Scoped Storage rules should not be operating.
Still I put "compileSdkVersion 29" and included in the Manifest file:
android:requestLegacyExternalStorage="true"
Still all my user has is an empty screen, while he could see an image before Android 10:
I had the same issue, and the combination of
<external-path name="pics" path="/" /> (used with FileProvider)
and
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
solved it for me (I am on SDK version 28).
Just wanted to say thank you for asking this question and providing the READ_URI_PERMISSION flag code, without that I couldn't get it working and I tried like 10 different things!
i think problem of image extension .jpeg,.jpg or other....
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(scannedUri);
startActivity(intent);
If you pass Uri like this way, For example
Uri imageUri = Uri.parse("http://www.android.com");
Intent imageIntent = new Intent(Intent.ACTION_VIEW, imageUri);
By default, the system determines the appropriate MIME type required by an intent based on the Uri data that's included.
Setting the MIME type further specifies which kinds of activities should receive the intent.
For instance, If you want to display an image using the ACTION_VIEW Intent, you should specify a MIME type of image/*. This stop (someone) from doing something in apps that can "view" other types of data (like a map app) from being triggered by the intent.
I hope it'll help you.

In Nougat Capture Image from Camera and get file from Uri

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.

E/PdfViewerActivity: fetchFile:file: java.io.FileNotFoundException: file does not exist

I'm trying to open a pdf after creating it via an intent. The file exists and is readable but save inside the apps directory.
Currently the file is saved in the following manner
OutputStream out;
try {
//TODO: expose through a content provider
out = mContext.openFileOutput(outputFileName, Context.MODE_WORLD_READABLE);
document.writeTo(out);
out.close();
and the file is sent to an intent with the following code
final Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
mActivity.startActivity(viewIntent);
The app trying to open it is the google drive viewer and I see in the Android monitor
E/PdfViewerActivity: fetchFile:file: java.io.FileNotFoundException: file does not exist
This previously worked and I don't think I've changed anything related to this code however I have updated my tooling. I've tried changing the buildToolsVersion in the build.gradle back to what it was before as well as the support libraries 'com.android.support:appcompat-v7:22.+' and 'com.android.support:design:22.+' instead of version 24.2.1
I've tried using the debugger to set the file to be readable just before sending the intent but that did not work.
Thanks
I found the answer starting with the Android 7 Behavior Changes documentation. Sharing files is no longer allowable when setting MODE_WORLD_READABLE even though the documentation doesn't mention any of this
I followed the documentation on sharing files to fix the issue but the sample code in the documentation was more complicated than what I needed. I still had to add the file provider to the AndroidManifest.xml as well as create the fileprovider.xml.
The resulting code is now
final Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
Uri fileUri = FileProvider.getUriForFile(mActivity, "my.authority.fileprovider", file);
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewIntent.setDataAndType(fileUri, "application/pdf");

Android photo picker for lollipop and above

I'm using the following code for picking photos:
Intent intent =new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 100);
and return the result with following:
InputStream inputStream = getContentResolver().openInputStream(data.getData());
Bitmap bmp1 = BitmapFactory.decodeStream(inputStream);
This works good on pre-lollipop but it says "Unfortunately the app has stopped" on lollipop and above. Do I need any permissions on lollipop and above? Has something changed since lollipop? Please guide me/ Suggest a simple method to pick photos on lollipop and above.
Use following way
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, Const.PICK_IMAGE_REQUEST);
OR use custom layout and activity
I have developed it.Please check below link working in all android version 4.0 or above. you dont need to ask for permission as its target version is 22.
https://github.com/rajscet/Photo_Picker_Git
Do I need any permissions on lollipop and above?
You should hold READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE. Note that on Android 6.0+, if your targetSdkVersion is 23 or higher, you need to use runtime permissions to request this permission from the user, as these permissions are dangerous.

Android gallery not updating photos & ACTION_GET_CONTENT issues

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!

Categories

Resources