Android 10 ACTION_VIEW won't show images outside scoped-storage - android

I've an image located at /storage/emulated/0/DCIM/Camera/xyz.jpg
And I want to open this image using ACTION_VIEW from my app.
The code I'm using is
File file = new File("/storage/emulated/0/DCIM/Camera/xyz.jpg");
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpeg");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
My provider paths have
<external-path name="external_files" path="." />
The above intent shows a picker to select gallery/photos app and system's gallery app gives a blank white screen with error File format isn't supported or files are corrupted while photos app just hangs on the loading screen.
So my question is: Is it possible to directly open a file(outside scoped storage) using ACTION_VIEW? Or Do I have to first copy the file to scoped storage and then show it with ACTION_VIEW?
Answer: It's not possible to use ACTION_VIEW to open files outside scoped storage. It's also not possible to copy the file since we don't have access to it. So, we can either use ACTION_OPEN_DOCUMENT to let user choose the file or we can copy the files to scoped storage and keep using ACTION_VIEW.

Related

How do I open a pdf with content URI on android >= 8?

I am trying to open a pdf with existing installed pdf apps such as Google PDF Viewer. But the PDF screen shows blank.
I created an intent and used ACTION_VIEW filter but when I open in Google PDF it shows just blank screen nothing else even name of the file in Google PDF is not visible only document id (content-provider://..../document/122 <- this id) is shown
String filePath = "content://com.android.providers.downloads.documents/document/346";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(filePath), "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mActivity.startActivity(Intent.createChooser(intent, "select an app to open this file"));
setFlags will replace all previous flags on intent. So use addFlags instead -
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
also check if you have long term access to content URI. After Android 4.3 you'll need to ask for persistent permission using takePersistableUriPermission() on content uri you get. Also you'll need to change ACTION_VIEW to ACTION_OPEN_DOCUMENT where you are getting the URI you mentioned for takePersistableUriPermission() to work.
Read below articles for more clarification -
- About Using ACTION_OPEN_DOCUMENT
- How to use new Storage Access Framework to access/modify/create a file

ACTION_GET_CONTENT does not allow moving or deleting files

I'm trying to use Intent ACTION_GET_CONTENT to open an application such as (File Manager, File explorer etc...). However once I am in the File Manager, I am only able to open the file. The Copy to.. and Move to... capabilities are not shown when opening File Manager app using this method. Is ACTION_GET_CONTENT only used for opening files?
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath());
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open folder"));
Here is the image showing the open option only:

video file cant play with Action_View uri got from Document file on removeable Storage

I could just get my files on removable storage by action_open_document tree and it gives me a document file. I can not open this file with Action_view.
The URI from document file did work!
I can open image file with action view .
String uri=DocumentFile.getUri.toString;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri);
intent.setDataAndType(Uri.parse(uri), "video/mp4");
activity.startActivity(intent);
Other apps do not have access to that content. Add FLAG_GRANT_READ_URI_PERMISSION to the Intent to pass along your own read access to the third-party app.

How to open the gallery using the file path in android

I have designed two buttons.One button for selecting the file and another button for to open the selected file.I have selected the file correctly and the file path also retrieved.But i cant open the particular file directly by the file path.Any one I have tried something like this
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ selectedFilePath);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
First, ACTION_GET_CONTENT does not accept a Uri as input.
Second, ACTION_GET_CONTENT has nothing to do with opening a file. Presumably, you should be using ACTION_VIEW.
Third, the value that you are passing to Uri.parse() is not a String form of a Uri.
Also, I would expect few Android devices to have an ACTION_VIEW activity for text/csv content.

Trouble Storing pdf in internal storage and viewing it from adobe

I have created a pdf file in my application and successfully stored that in sdCard, now i want to store the pdf not in my sdcard but in Internal storage as shown here but when i open an intent for pdf readers as:
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
Log.v("Path",file.getAbsolutePath()); // Path data/data/com.cloudchowk.his.patient/files/LIPID PROFILE1300000403.pdf
i.setDataAndType(uri, "application/pdf");
startActivity(i);
The PDF reader shows an error The document path is not valid.
I have tried this on two devices one was rooted and one was not, but it was not working on any of them. What should i do ? any ideas?
Get the path and create URI
File internalFile = getFileStreamPath("pdf path");
Uri file = Uri.fromFile(internalFile);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(file, "application/pdf");
startActivity(i);
To make a file from internal storage available to other apps the cleanest approach is to use a content provider.
Luckily, Android comes with FileProvider – an implementation that might just serve your needs.
You don't need to implement code, you just need to specifiy the provider in your manifest.

Categories

Resources