how to read pdf files from res folder in a android studio - android

Can anyone help me. I want to read pdf files from app/src/main/res/Unit1.pdf.My code is
File file1=new File("app/src/main/res/Unit1.pdf")
Uri path1=Uri.fromFile(file1);
if (file1.exists())
{
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path1, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(MainActivity.this,"No Application Availabel To View
PDF",Toast.LENGTH_SHORT).show();
}
}
if section not working....plz help..

You need to write the your PDF file present in your app's res folder to sdcard. So that other apps can read and open it.
As in res folder file is locally available to your app only. and you are Throwing an intent for other apps to open that pdf which is not accessable to them. so remove your if check, write your pfd from res to sdcard and pass that file's uri to the intent.

Related

How to open a specific file or a folder in Android 10?

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 want to read the pdf files from assets or raw folder in android

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.

File not exist occurs only for XLSX file in android

I have trying to open XLSX file in android using intent filter to select existing XLSX reader application but always getting file not found exception. The same code works fine for XLS file. I added WRITE_EXTERNAL_STORAGE permission in manifest and file path being correct only. File also available in SD Card. My tablet have sheet to go and OfficeSuite application to read XLSX files. Even i can able to open my XLSX file by clicking on it. But trying to open via my application gives problem. For XLSX file my code always goes to else part. Thanks in advance.
private void openSpreadSheet(String fileCompletePath) {
File file = new File(fileCompletePath);
if (file.exists()) {
try {
Log.d(TAG, "File exist");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(this,
"No application found to display spread sheet.",
Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "File not exist");
}
}
If i remove if condition, then intent filter asked for choose application after choosing application, that selected application shows this error File not found(/Duplicate.xlsx:open failed:ENOENT(No such file or directory)).
Found Solution
Issue comes because I gave wrong file path.

reading file in assets or raw folder in Android

I have to ship pdf files along with my application and display them as list and when clicked each one should open in Adobe reader. I have tried placing the pdf files in /assets folder also in /res/raw folder. But in either case I am encountering ActivityNotFound Exception. The code and the exception message are below.
Placing file in /assets/pdf folder
try {
files = amanager.list("pdf");
//AssetFileDescriptor fd = amanager.openFd(files[0]);
Uri path = Uri.parse("android.resource://com.csg.android.myproject/assets/pdf/csgsample") ;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage("com.adobe.reader");
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(FirstTab.this, "NO Viewer", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Placing file in /res/raw folder
Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample") ;
Exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.csg.android.myproject/assets/pdf/csgsample typ=application/pdf flg=0x4000000 pkg=com.adobe.reader }
Any help would be greatly appreciated.
Thanks in Advance,
Navin
If you get an ActivityNotFoundException is because there are no application to handle that kind of content. So, in that case, I'm sure the the Adobe Reader is installed.
Also, I think you must copy the file to the SDCard for other apps to access it. They cannot access your private data directly for security reasons.
I think assets and raw files are only viewable by your application. 3rd party applications (bundled or not) cannot access these files. You should first put your files in the device's sdcard before opening them.
I have the same problem, but I should note that this is how I open by resource id
Uri.parse("android.resource://com.csg.android.myproject/" + R.raw.csgsample);

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