Where a downloaded file is stored and where Uri goes to pick it up are not the same.
Java.IO.File file = new Java.IO.File(Query.filePathh);
Console.WriteLine("Downloaded file PATH: " + Query.filePathh);
Intent open = new Intent(Intent.ActionView);
open.AddFlags(ActivityFlags.GrantReadUriPermission);
open.SetFlags(ActivityFlags.NewTask);
Context context = Android.App.Application.Context;
Android.Net.Uri fileUri = FileProvider.GetUriForFile(context, "com.companyname.Login.provider", file).NormalizeScheme();
Console.WriteLine("File uri: " + fileUri.Path);
open.SetDataAndType(fileUri, "*/*");
Intent intentC = Intent.CreateChooser(open, "Open With");
intentC.AddFlags(ActivityFlags.GrantReadUriPermission);
intentC.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intentC);
We get pop-up that asked how to open a file, but when the application tries to open it, it crashes with an error that the file path does not exist.
While debugging we tried to see file locations on where the file is downloaded and what Uri is calling and we get:
For storage_path:
storage/emulated/0/Download/How_to_initialize_your_Xamarin_app_to_use_AppConnect_C#_APIs.pdf
For Uri path:
/external/Download/How_to_initialize_your_Xamarin_app_to_use_AppConnect_C#_APIs.pdf
We can open the file normally if we go to Download folder in our Emulator.
Any suggestions on what to do?
Related
I'm trying to open the download directory in my emulator using Intent.ACTION_GET_CONTENT.
I can successfully get the path of the downloads directory and list all the files in it, but I can't seem to open it with an intent. It only displays the Recent directory.
Successfully logs all the files in that download directory, so the path to it is not incorrect
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
File[] files = f.listFiles();
if (files != null) {
for (File inFile : files) {
Log.d(TAG, "onPermissionGranted: File name: " + inFile.getName());;
}
}
But when using the intent:
public void openDirectory() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
Log.d(TAG, "openDirectory: " + uri);
//logs this: /storage/emulated/0/Download
intent.setDataAndType(uri, "file/*");
startActivity(Intent.createChooser(intent, "Select keystore"));
}
It always displays Recent
Instead of Downloads
I have to manually go to the Downloads tab
How can I make it so that the intent goes to the Downloads without having the need to switch?
This is how you do it:
Intent intent=new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(intent);
I have downloaded a file (133465.pdf) using the Download manager and now it is stored in the Downloads folder of the mobile phone (Internal storage).
How should i try and retrieve the downloaded pdf from the Downloads folder?
I am using the below code to try and retrieve the pdf from the downloads folder but i am getting an error on the Toast, saying "Cannot display PDF (133465.pdf cannot be opened)" .
String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator + "133465.pdf";
Log.i("Fragmentadapter1", file);
File videoFile2Play = new File(file);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(videoFile2Play), "application/pdf");
imageContext.startActivity(i);
I don't know if i am using the right file location to access the file.
Any help or suggestion will be appreciated.
If you are working for Lollopop and Below: You don't have to ask the user for permission on run-time. The manifest Permissions will do.
If you are working for Marshmellow and up: You have to ask the user for permission on run-time and act according to the users output.
Remember: You still have to give the Permissions on the Manifest too.
To Download a PDF on users Downloads folder :
DownloadManager downloadmanager;
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
downloadmanager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
String url = hardcode + bb ;
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri)
.setTitle(bb )
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
bb)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Log.i("Download1", String.valueOf(request));
downloadmanager.enqueue(request);
Reference
To View the Downloaded PDF from the Downloads folder of the Users device:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
"YOUR FILE NAME");
Uri path = Uri.fromFile(file);
Log.i("Fragment2", String.valueOf(path));
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(path, "application/pdf");
try {
this.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
}
Note: Make sure u get the permission granted before downloading the Viewing the PDF file for Marshmellow and UP.
I have a function that downloads and extracts a zip file.
Its extracted to the external storage in: ../Android/data/packagename/..
When I try the following intent to view a .mp4 video for example, its opened in a video player and says that it can't open the file. (not only .mp4 files)
Uri uri = Uri.parse(Helper.getStorageDir(getActivity()) + "/" + mediaObject.getLinkOffline());
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(mediaObject.getLinkOffline().substring(mediaObject.getLinkOffline().lastIndexOf(".") + 1, mediaObject.getLinkOffline().length()));
newIntent.setDataAndType(uri, mimeType);
newIntent.setFlags(newIntent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(newIntent);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(getActivity(), "No handler for this type of file.", 4000).show();
}
However, when I go through a file explorer and open the same file there is no problem.
I have there read/write permissions in my manifest. But thats obviously not the problem since I can read write the file and can also check the the file exists. It just wont let an external app open the file through an intent from my app.
Am I missing something?
EDIT: when I debug and check the mime type for the mp4 file it is "video/mp4".
I have this code:
File file = new File("android.resource://" + getPackageName() + "/" + R.raw.intro_sad);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
it results from an error in file path. how to get the filepath?
I don't think raw resources are accessible by external applications (which is what I think you're trying to do). Instead you should copy the file to an external storage or a WORLD_READABLE folder and then use the path of the copy.
I am trying to send file attached email from my App.
The file saved into external storage(SDCard) can successfully attached, but the same file that is saved into temporary directory where I can get getCacheDir() method cannot be attached.
The only difference is to where the file I want to attatch is saved,
Is this because of an Android spec or limitation, or am I missing something?
I'm using ACTION_SEND intent to send attachment file via email
//// getting file path to save
//[fail] -- no attatchment in email
//path = new StorageUtil().getCacheFilePath(this, "attatchment.html");
//[success] -- attatchment.html is on email
path = new StorageUtil().getExternalAppStoragePath(this, "attatchment.html");
/// start intent if file saving is successful
if (this.export(path)==true) {
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("text/html");
i.putExtra(Intent.EXTRA_SUBJECT, "a subject");
i.putExtra(Intent.EXTRA_TEXT, "");
File f = new File(path);
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(Intent.createChooser(i, "Send Email"));
}
getCacheFilePath() creates path from following code where fileName is the second argument of the method:
File cacheDir = ctx.getCacheDir();
File cacheFile = new File(cacheDir, fileName);
return cacheFile.getPath(); //path
each path is as follows
//cache dir (attachcment failed):
/data/data/{PACKAGE_NAME}/cache/attachment.html
//external dir (attachment successed):
/mnt/sdcard/Android/data/{PACKAGE_NAME}/files/attachment.html
File object from the cache dir canRead() and could obtain file length.
thanks!
== SOLVED ==
I found that the following error is on Logcat when sending Gmail:
file:// attachment paths must point to file:///mnt/sdcard. Ignoring attachment file:///data/data/{PACKAGE_NAME}/cache/attachment.html"
So this should be a Android limitation. Adding Intent.FLAG_GRANT_READ_URI_PERMISSION has no effect and other Activity like Dropbox results similar.
I suggest you to use getExternalCacheDir() instead of getCacheDir(), which point to "/storage/emulated/0/Android/data/". File under this folder seems can be attached to gmail.