Android intent doesn't work on .apk files - android

I'm developing a File Manager, and I open files this way:
fileName = fileToOpen.getName();
fileName = fileName.substring(fileName.lastIndexOf('.') + 1);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", fileToOpen);
intent.setDataAndType(uri, MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileName));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
This can handle almost all extension, but when I click on .apk files an ActivityNotFoundException gets thrown. I would like to launch the android installation screen.

Only on Android 7.0+ will the package installer know how to work with content Uri values. For earlier versions of Android, you have no choice but to have the APK on external storage somewhere and use a file Uri.

Related

Show a different filename when opening file with ACTION_VIEW intent on android

I have a file stored in my App's storage like this:
/storage/emulated/0/Android/data/com.example.myapp/files/App files/90210_John_s_Resume.pdf
the 90210 at the beginning is a file ID for internal purposes only, and I give the user the option to save this file to Downloads folder, where I remove the 90210 prefix. But I also provide the user to preview the file using ACTION_VIEW intent. This causes an issue where the file name in the previewed app still has the 90210 prefix. Is there a way to overcome this?
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri fileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", finalFile);//NO I18N
intent.setDataAndType(fileUri, URLConnection.guessContentTypeFromName(fileUri.toString()));
context.startActivity(intent);

Android Sharing files without FileProvider

I have tried to share a file using the Android Sharesheet, and the guide says I should use the FileProvider.
But in my situation, I can't use the FileProvider due to APK size.
Is there any way to share a file via the Android Sharesheet without using the FileProvider?
A file that I want to share is in the external storage such as "/sdcard/abc/file.zip".
I have already tried this, but it didn't work.
Uri uri = Uri.parse("file://" + file_path); // FileUriExposedException
// Uri uri = Uri.parse("content://" + file_path); // when sharing via GMail app, a toast appears with message "Couldn't attach file"
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/zip");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Share"));

Open folder on card

I thought this was something really simple, but it's giving me more work than the rest of the whole app.
I just want a button to open the "Downloaded Files" folder of my app. That's all. No file picker, nothing complicated. All the button has to do is open a folder with the default app (or open the app chooser if there's no default).
I tried different solutions I saw here on StackOverflow, but nothing seems to work for me.
Example 1:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
intent.setDataAndType(uri, "text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));
On my Android 4.4, this opens the KitKat file picker, and doesn't even open in the folder I want... it seems to be defaulting to the card root. But I'm sure the folder exists and the path being given to the Intent is indeed correct.
Example 2:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
intent.setDataAndType(uri, "text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));
This one opens some blank activity with a popup saying: "Error occurred when getting file Content: MyDownloadedFiles".
How can I make my app to simply have a shortcut to a folder where it puts contents into?
wow.. Finally!! After many things I tried, the only way it worked was by wrapping the URI string into a File first, and then do a Uri.fromFile:
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));
I think this one could work for you:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/yourFolder/");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open /sdcard/yourFolder"));

I need to open my .doc file in quickoffice in my app without looking for suitable apps (options)

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);

Opening PDF externally with Polaris office 5 returns "This document cannot be opened"

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);

Categories

Resources