I have created a App in which I am showing the pdf and docx file in Android Device which get download in device when button is been clicked we are displaying it in devices why using below code.
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/FILE/a.docx");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path,"application/msword");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(pdfIntent);
Getting an error message in Android 4.2:
file read error. File type is unsupported or the file is corrupted.
That is not the correct MIME type for DOCX files. That is the MIME type for the older DOC format. Try application/vnd.openxmlformats-officedocument.wordprocessingml.document.
Also, do not use concatenation to build file paths, as you are making assumptions about the value returned by getExternalStorageDirectory(). Use:
File pdfFile = new File(Environment.getExternalStorageDirectory(), "FILE/a.docx");
Related
I'm trying to get file from google drive using ACTION_GET_CONTENT.
When I clicked the file, then the results are:
uri.getPath() : "/document/acc=1;doc=encoded=K45Kn19bK1yXpNssYJAhfwJ/4hgc5VmQJk2FWtVNoeM+c1vHUO4H"
uri.toString():"content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3Dencoded%3DK45Kn19bK1yXpNssYJAhfwJ%2F4hgc5VmQJk2FWtVNoeM%2Bc1vHUO4H"
File recoveryFIle = new File(uri.toString());
file size : 0
How can I get the real file? I select the file from Google Drive.
getfile
Intent recovery = new Intent(Intent.ACTION_GET_CONTENT);
recovery.setType("*/*");
recovery.putExtra(Intent.EXTRA_SUBJECT,"Savelocation_Backup");
startActivityForResult(Intent.createChooser(recovery, "Share File"),3002);
result
Uri uri = data.getData();
Log.d("TAG",uri.getPath()+"");
Log.d("TAG",uri.toString());
File recoveryFIle = new File(uri.toString());
Log.d("TAG",recoveryFIle.length()+"");
No. Impossible.
Stop trying.
Moreover you need not what you call a file.
I'm trying to open a docx file in Word on Android using an Intent, but it always opens Read Only (Word prompts to say it is read only).
The file IS read-write in other apps that can edit a docx (e.g. WPS Office). Only Microsoft Word is the issue.
String fileUrl = "/storage/emulated/0/Download/test.docx";
File file = new File(fileUrl);
String mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Uri theUri = FileProvider.getUriForFile(this,
"com.example.testint.fileprovider",
file);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.setDataAndType(theUri, mime);
startActivity(intent);
I have run an Intent Intercept with several other apps. ASUS File Manager and ASTRO file manager and File Manager 1.36 are all able to open Word documents as Read/Write using content:// and do not have the read only issue. The intents look the same as my intent, so I cannot see how they can work, when this does not.
This is from ASUS file manager:
intent://com.asus.filemanager.OpenFileProvider/file/sdcard/Documents/Test.docx#Intent;scheme=content;type=application/vnd.openxmlformats-officedocument.wordprocessingml.document;launchFlags=0x3000000;end
------------
ACTION: android.intent.action.VIEW
DATA: content://com.asus.filemanager.OpenFileProvider/file/sdcard/Documents/Test.docx
MIME: application/vnd.openxmlformats-officedocument.wordprocessingml.document
URI: intent://com.asus.filemanager.OpenFileProvider/file/sdcard/Documents/Test.docx#Intent;scheme=content;type=application/vnd.openxmlformats-officedocument.wordprocessingml.document;launchFlags=0x3000000;end
FLAGS:
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_PREVIOUS_IS_TOP
Has anyone else here managed to open Word documents for read AND write ok?
You can use the old file format e.g. file:///storage/emulated/0/Android/data/.../hello.docx or a content provider e.g. content://org.cryptomator.fileprovider/external_files/Android/data/org.cryptomator/cache/decrypted/hallo.docx BUT the file is not allowed to be in the app's internal storage. Then the files are writable!
Files with e.g. the .doc extension aren't writable or files located in internal storage aren't writable as well. Here you can find a short overview: https://github.com/cryptomator/cryptomator-android/issues/150#issuecomment-514401775
I need to open a PDF. I know the procedure, this is my code:
string file_path = _path + url.Substring (5);
if (System.IO.File.Exists(file_path)) {
Android.Net.Uri pdfFile = Android.Net.Uri.FromFile (new Java.IO.File (file_path));
Intent pdfIntent = new Intent (Intent.ActionView);
pdfIntent.SetDataAndType (pdfFile, "application/pdf");
pdfIntent.SetFlags (ActivityFlags.NoHistory);
_parent.StartActivity (pdfIntent);
return true;
}
I'm using Xamarin, and the path exists because i check it as you can see.
The app opens Adobe reader, but when it starts an error message shows up saying (File not found). So, my file is in
/data/data/com.myapp/files/.hide/contents/file_test.pdf
Are there some permission to set? I really don't understand why it can't open my file!
The folder /data/data/com.myapp is the private folder of your application. So no other application can access to the content of this folder (in this case Adobe reader).
Instead try to put your pdf into a SDCard folder.
I intend opening a PDF file with the default application of my device. I have a problem giving address from the assets folder.
My giving address code is as follows:
File pdfFile = new File("file:///android_assets/test.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
This code gives the error This path is not valid when Adobe Reader opens. What's the correct address?
It shows the error because only your application have the permission to access that folder. You can think to copy the file in a different directory (e.g. some place in the sdcard) and provide acrobat reader with the uri to the copied file,
I have to build an Android application which shows a list of pdf files. These pdf files should be secured, in other words - the user of the app should not be able to get a copy of the pdf content by any means (copy/cut/print...etc). My questions now are
How should I ship the content of the pdf file along with .apk file.
If we send the content of the file in a diff format (raw/byte code), how should I convert the file back to pdf and where should I place the file on the installed machine such that it is secure.
FYI, I will be locking down current version of Adobe Redaer as the pdf viewer to view the pdf files as it doesn't allow copy/paste/print.
This is the first Android app that I am developing. So, I'm a kind of Android newbie. Any help would be greatly appreciated.
Thanks,
Navin
Probably you should store it in the res/raw folder
You can copy this(these) pdf file(s) into assets folder, thus your files will be the part of your application and can not be accessible outside, and you can also treat them as normal files (i.e. open with any pdf viewer in your case). Whenever you want to get access to context file you can call context.getAssets() .
Hope this information helps.
Update : Here is the code I am using to open a file in downloads directory, this code can open the pdf file in any compatible pdf reader if installed on phone, Tested with Adobe Reader. You will need a URI of the pdf file in assets folder to run the code.
String fileName = pdfUrl.substring(pdfUrl.lastIndexOf("/"));
Log.i("fileName", "" + fileName);
File file = new File("/sdcard/download" + fileName);
// File file = new
// File("/sdcard/download/airtel_latest000.mp3");
Intent intent;
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
downloaded = true;
} else {
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(pdfUrl));
downloaded = false;
}
try {
startActivityForResult(intent, 5);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}