I am working with pdf files in my android app.
In a Alert Dialog pressing "Yes" should open the pdf file location in default file manager app.
I tried with this but it always open a chooser. Even-though it(ES File Explorer) opens with "Choose Path" dialog not the folder in the app as normal way of accessing folder.
So, is there possible to open directly the content of folder in "Default File Manager"? Or can open with chooser , from that selected app it should show the pdf file chooser when selecting the pdf file?.
Below is my try to achieve this.
public void openFolder(String filePath)
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(filePath.substring(0, filePath.lastIndexOf("/")));
intent.setDataAndType(uri, "*/*");//tried with application/pdf and file/*
intent.addCategory(Intent.CATEGORY_OPENABLE);
PackageManager pm = context.getPackageManager();
List<ResolveInfo> apps =
pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (apps.size() > 0)
startActivity(intent);//Intent.createChooser(intent, "Open folder"
}
Edited
And I tried with ACTION_PICK too. Its directed to specific folder of default file manager.But from there selecting a file not open the chooser (pdf viewer list dialog).
Related
I want to show downloaded file in File Manager by giving the File absolute Path.I read a lot about it and find same thing
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(file.getAbsolutePath());
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open folder"));
And it results to view only recent files that has been viwed. Please guide to Open File Manager while navigating it to file whose absolute path has been given.
I read a lot about it and find same thing
ACTION_GET_CONTENT has little to do with a file manager.
Please guide to Open File Manager while navigating it to file whose absolute path has been given.
There is nothing on Android for this, sorry.
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:
I am implementing a file chooser in my android app, I am having 3 different file chooser each for AUDIO, VIDEO, IMAGE.
Its working good below android 7.0, when clicked, its going to gallery for choosing that type of file.
But in Android 7.0, when i am clicking to choose the file picker, its going to RECENT and from there manually i need to go to audio or image gallery, how can i directly go to audio gallery on clicking file chooser as its working in kitkat.
Below is my code to call the file chooser --
private void showFileChooser() {
Intent intent = new Intent();
//sets the select file to all types of files
intent.setType("audio/*");
//allows to select data and return it
intent.setAction(Intent.ACTION_GET_CONTENT);
//starts new activity to select file and return data
startActivityForResult(Intent.createChooser(intent,"Choose File to Upload.."),PICK_FILE_REQUEST);
}
I need to open my .doc file in quickoffice in my app without looking for suitable apps (options).
Here I have used this
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
//String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
// MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
//intent.setDataAndType(uri, type == null ? "*/*" : type);
intent.setDataAndType(uri, "application/msword");
startActivity((Intent.createChooser(intent,
getString(R.string.open_using))));
This Code shows suitable target apps which i dont need actually
Remove the createChooser call, and just pass the intent to startActivity. Then it will open the default app for the mime type, assuming one is set. If none is set, it may still pop up a chooser among those apps which claim they can open it. If you want to only open quick office you can do so by activity name, but then it will fail (and possibly throw an exception) if quick office is not installed.
It worked for me.
PackageManager packageManager = getPackageManager();
Intent quickOffice = packageManager
.getLaunchIntentForPackage("com.quickoffice.android");
File sdcard = Environment.getExternalStorageDirectory();
//your file location
File file = new File(sdcard, "Meta Data.doc");
Uri path = Uri.fromFile(file);
quickOffice.setAction(Intent.ACTION_VIEW);
quickOffice.setDataAndType(path, "application/msword");// ---->application/msword
startActivity(quickOffice);
We've been trying to solve this issue for the past few hours, finally decided we should revert to StackOverflow.
Here goes -
We have an application that downloads a PDF file from a server to the app's cache directory and then opens it using the packet manager. Of course it goes through grantUriPermission() to give read (and write) permissions to all available packages.
Though it works great on most devices, today we encountered a device that has POLARIS Office 5 installed as the default PDF viewer.
Whenever we're opening the file, Polaris simply displays a message saying "This document cannot be opened".
I should say that when trying to open the file through Acrobat Reader, it works great.
Also, when we copied the file from the cache directory to an external directory (using Android's File manager) and then opened it in Polaris manually it worked great.
We would have given up on it, but since Polaris is the default viewer on many devices, we really would love solving that problem.
Here is the code -
public void onDownloadDone(String filepath) {
// Set a file object that represents the downloaded file
File file = new File(filepath);
// Set an intent for the external app
Intent intent = new Intent(Intent.ACTION_VIEW);
// Get mime type of the downloaded file
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(filepath);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
intent.setType(mimeType);
// Look for installed packges according to the file's mime type
PackageManager pm = context.getPackageManager();
Uri contentUri = FileProvider.getUriForFile(context, "myapp.fileprovider", file);
// Set the file uri in the intent
intent.setData(contentUri);
// Give permissions to the file to each external app that can open the file
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
for (ResolveInfo externalApp: activities){
String packageName = externalApp.activityInfo.packageName;
context.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
// Start the activities (or launch the menu) if any activity exists
if (activities.size() > 0){
context.startActivity(intent);
} else {
System.out.println("Warning!!! No app for file " + filepath);
}
}
Thank a lot!
I had exactly the same problem. The PDF files would open with ezPDF and Adobe but not with Polaris Viewer.
You have to set the data and type with:
intent.setDataAndType(uri, "application/pdf");
instead of using:
intent.setType("application/pdf");
intent.setData(uri);
For me the following is working fine with Polaris now:
Uri uri = FileProvider.getUriForFile(MyApplication.getContext(), MyApplication.getContext().getPackageName() + ".myfileprovider", destFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);