I'm trying to open a specific folder using intent, but the device's recent folder open instead.
Code:
Uri uri = Uri.parse(filepath);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT,uri);
intent.setDataAndType(uri,"text/plain");
StartActivityForResult(intent, REQUEST_CODE);
ACTION_GET_CONTENT does not take a Uri, so yours is ignored.
With ACTION_OPEN_DOCUMENT, you can use EXTRA_INITIAL_URI to provide some document tree (e.g., from ACTION_OPEN_DOCUMENT_TREE) that should be used as the initial location.
Related
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
I am trying to read pdf files in the android app.
Fire following intent to get URI.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(intent, PDF_FILE_SELECTOR_INTENT_ID);
Problem is, the Downloads folder also shows old files that I have deleted.
Also, when I select those files a valid URI is returned in onActivityResult(). When I create File from URI and check exists() it returns false which makes sense as I have already deleted the file from the Downloads folder.
How can I make sure that the Downloads folder shown on ACTION_GET_CONTENT shows only files which are currently present and not deleted ones?
Thanks.
Instead of
intent.setType("application/pdf"); use the Intent.EXTRA_MIME_TYPES in the putExtra
In Java:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {
"application/pdf", // .pdf
});
startActivityForResult(intent, REQUEST_CODE);
In Kotlin
startActivityForResult(
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf(
"application/pdf", // .pdf
))
},
REQUEST_CODE
)
Update: Actual Download Folder & "Downloads" folder different in this case.
Download is for your actual downloaded files
Downloads is a history folder behaves like a short-cut and it's not cleared automatically when the actual file pointed to is manually removed. This might be most likely an expect behavior.
In your case , you need to hide the downloads folder ( still you can use Download). using this line of code will show "Internal storage" by default:
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
Call addCategory(Intent.CATEGORY_OPENABLE) to your Intent as recommended here: https://developer.android.com/reference/android/content/Intent#ACTION_GET_CONTENT
In my app I want to give users a way to pick a file from the app’s data directory. This is my code:
// use ACTION_OPEN_DOCUMENT because ACTION_GET_CONTENT will give us
// gallery and other stuff we don’t need
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = Uri.parse(getExternalFilesDir(null).getAbsolutePath());
Log.d(TAG, "Browsing " + uri.toString());
intent.setDataAndType(uri, "*/*");
// show the entire internal storage tree
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
The logcat shows me that the URI that I am setting is file:///sdcard/Android/data/my.app/files, but the file picker UI defaults to the shared storage root (/sdcard).
The following code works (requires API 26+ as per the documentation, the intent is available from the API as DocumentsContract.EXTRA_INITIAL_URI):
// works only with this intent, at the expense of gallery etc. appearing
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// apparently we need a valid content URI
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fmy.app%2Ffiles");
intent.putExtra("android.provider.extra.INITIAL_URI", uri);
Log.d(TAG, "Browsing " + uri.toString());
intent.setType("*/*");
// show the entire internal storage tree
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri);
However, ACTION_GET_CONTENT causes all kinds of providers to appear, such as Gallery and Music, when all I need is the local file system (and in fact just the app’s private subtree). If I change the intent to ACTION_OPEN_DOCUMENT, the URI I supply is ignored.
How can I get the file picker UI to start in a directory of my choice, with only a minimal choice of content providers?
Edit: Testing this on Anbox, which I just realize is only at API 25—in fact, I need a way that works on APIs as low as 24.
There may not be a universally viable solution, but the following has worked on some builds (though not on others):
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fmy.app%2Ffiles");
intent.setData(uri);
intent.setType("*/*");
intent.putExtra("android.provider.extra.INITIAL_URI", uri);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
uri must be a content URI for the com.android.externalstorage.documents provider. The URI path is /document/primary%3A, followed by the filesystem path to the folder to start in. The path must be relative to the shared storage root (i.e. drop the leading /sdcard/ or equivalent on the device and make sure the result does not start with a slash) and escaped.
The call to Intent#setData() does not help in setting the default location (unlike with some third-party file managers) but prevents unwanted storage providers (such as Gallery and Music) from being displayed.
The android.provider.extra.INITIAL_URI extra sets the initial URI, but this may not work prior to API 26 (although it does work on some flavors of Android).
The android.content.extra.SHOW_ADVANCED extra causes device storage to be available as a provider (otherwise, depending on the flavor of Android, it may require the user to select it or not be available at all).
Again, still not a perfect solution but the closest I managed to get.
I want to open the default gallery app to a folder (named fooo) in the primary storage. I thought the easiest way would be to use Intents. When I use this code for my intent, the gallery app opens but it shows no image. I check with file explorer and there are images.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/fooo/");
intent.setDataAndType(uri, "image/*");
startActivity(intent);
First, there is no requirement for any ACTION_VIEW activity to be able to handle a directory as the Uri.
Second, your MIME type is incorrect, as a directory is not an image.
Third, your Uri is invalid, because it lacks a scheme. Replace:
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/fooo/");
with:
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "fooo");
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.