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();
}
Related
I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app.
I have tried this:
try {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
File f = new File(getExternalFilesDir(null) + "/MyFolder");
Uri uri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName()+".provider",f);
Toast.makeText(MainActivity.this,f.getPath(),Toast.LENGTH_LONG).show();
intent.setDataAndType(uri,"text/plain");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
catch (Exception e) {
Log.d("Exception",e.toString());
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
But, it opens the entire tree.
What I actually want is to open that folder. The files in the folder should be openable by other apps. Any help will be appreciated.
I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app
Sorry, but that is not an option.
The closest thing to what you want is EXTRA_INITIAL_URI. However:
It takes a document or tree Uri that you obtained previously from the Storage Access Framework, not a FileProvider Uri; and
It does not prevent users from navigating elsewhere — it just controls the starting point
I am trying to open up PDF using intent that takes the following:
Here is the code that I have:
Intent intent = new Intent(Intent.ActionVIEW)
intent.setDataAndType(Uri.parse("CONTENTURI + FILENAME","application/pdf"
try { startActivity(intent)} catch excpetion and so on.
It pops up with whatever applications I have installed, from Adobe Reader, Google PDF reader, POLARIS(as I am using Galaxy Tab 3 for testing), and none of those work. Each say unsupported document.
Does download and show the right solution in this case, or any ideas? Thanks in advance!
Following code triggers the VIEW action for pdf files (open the default PDF viewer, choose between applications capable to view PDFs or get an error if you don't have any installed).
File file = new File("your pdf path here");
if (file.exists()) {
Uri pdfPath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfPath, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
//if user doesn't have pdf reader instructing to download a pdf reader
}
}
NOTE: The PDF should not be saved local to application, or else the third party app won't have access. You should use media location.
when i m run it i m getting this error like "File or folder not found" and " file dosnt exit or can't be opened "..
This is the code sir..
i want the how to view pdf files from assets or in raw folder sir.. its very imp for my project try to slove my problem sir.. and tell me clearly i m new to android sir.. can any one send me code cloerly how to do it..
enter code here
Uri path = Uri.parse("android.resource:///com.example.pratice/raw/em");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(this, "NO Viewer", Toast.LENGTH_SHORT).show();
}
Few, if any, Android PDF viewer apps will honor the android.resource scheme, and there is no Uri scheme for assets.
If you want to serve up a PDF file, copy it from assets or a raw resource into internal storage, then use FileProvider to make it available to other apps. Here is a sample application that does this.
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.
able to open the pdf files . but dont know how to save the content of the pdf file in a array or something then i can show the pdf as i want. can anybody suggest me how to save the pdf content...is there any libraries to use?
following snippet shows how to open a pdf
File file = new File("/sdcard/example1.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Log.e("path of the file",path.toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(),
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
Thanks
You should use iText library..
iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them
here is the link.
http://www.vogella.de/articles/JavaPDF/article.html
Take a look at Mono for Android. It will enable you to develop for Android using C# and .NET and will let you install and access to the iTextSharp library
The iTextSharp is a very extensive and well documented library for creating and manipulating adobe pdf documents.