Android Open PDF Intent - android

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.

Related

How can I pass a PDF to Adobe Reader on Android by intent to edit ist directly and not a copy?

I use the following code to open a pdf form. If I use Acrobat Reader it's not possible to write back to the original file. It always makes a copy which I only could guess but newer know for shure in my app.
I know that it has to be possible to edit the original because if I open a pdf from any file manager (e.g. the one from Asus) Adobe Reader edits it directly.
Xodo PDF allows direct edit. It even supports the correct ACTION_EDIT intent but I'm afraid that our users insist to use Adobe...
How can I edit the original with Adobe?
final File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) + File.separator + "x.pdf");
if(file.exists())
{
file.setWritable(true,false);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri = FileProvider.getUriForFile(Objects.requireNonNull(getContext()),
BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setDataAndType(contentUri,"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent = Intent.createChooser(intent, "Open File");
startActivity(intent);
}
At the moment even Xodo doesn't work anymore but I was lucky to realized that OneDrive-PDF-Viewer does.

PDF viewed can't open file - but only on tablet

I have an app that among other things fires up a pdf reader to allow the user to view a document selected by the app. It works on my phone but not Samsung tablet. The intent seems to work fine and the selection of reader apps comes up but when the reader is selected, a short time later the error message” Can’t open file “ is shown.
The same app can also fire up browser and text applications to show other files and this works fine on the tablet. So my file references are all OK. External storage for WRITE in the manifest is set OK.
When I select the document on the tablet (not thru my app) it opens OK. I have selected the pdf part of the main app and extracted it into a simple pdf reading only app and still the same problem.
It seems to be specific to the tablet – can anybody help me?
Intent i = new Intent(Intent.ACTION_VIEW);
File file1 = new File(Environment.getExternalStorageDirectory() + "/Documents/A.pdf");
Uri ur=Uri.fromFile(file1);
i.setType("application/pdf");
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i) ;
Instead of this code
Intent i = new Intent(Intent.ACTION_VIEW);
File file1 = new File(Environment.getExternalStorageDirectory() + "/Documents/A.pdf");
Uri ur=Uri.fromFile(file1);
i.setType("application/pdf");
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i) ;
Use this way
File pdfFile = new File(Environment.getExternalStorageDirectory(),"namePdfFile.pdf");//File path
if (pdfFile.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);//Staring the pdf viewer
} else {
Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();
}
To Read PDF in Browser Use below code :
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
Thanks to all for help. Actually I was just confused by the mime types. Read up on this and all is well. App working perfectly along the lines posted by Ironman

Unable to share image in android

I am new in android Java.
I am trying to share an image in android application using Phonegap. I get new class "Share" as CordovaPlugin, code is follow...
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Uri uri = Uri.parse("file:///android_asset/www/sharethis.png");
share.putExtra(Intent.EXTRA_STREAM, uri);
cordova.getActivity().startActivityForResult(share, 0);
Above code is not working, it showing like this ...
is think i cant get Exact image file location.
My file location
I tried bellow code also, But not work,
String imagePath = Environment.getExternalStorageDirectory()
+ "/assets/www/sharethis.png";
Android asset folder is private to your app so you can't share files from there directly. You need to copy the file from assets to a public directory in the filesystem and then send the share intent pointing to the public file

How to giving address from assets 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,

Pdf files as part of Android .apk

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

Categories

Resources