Access PDF file in assets folder on android - android

I have seen many question about accessing files in assets folder but can't seem to get a solid answer. I'm working on a application that would extract a text from PDF file thus I'm using iText Library to do that but my problem here is the file pathing of PDF.
I have tried using assetManager and assetManager opens that file but i think iText needs the file path so it can open the file by itself, i am not sure but that is my theory.
PdfReader reader = new PdfReader(<String PDF-file>);
then How do I access the file under assets folder using iText? if it is not possible is there a way to do that?

Here is sample code to get asset directory for file and use it for pdfreader.
code:
File f = FileUtils.fileFromAsset(this, "filename.pdf");
PdfReader pdfReader = new PdfReader(f.getPath());

You can get an InputStream object like this
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
and then use this constructor
public PdfReader(InputStream is)
throws IOException
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#PdfReader(java.io.InputStream)

I have solved my problem my using this:
reader = new PdfReader(getResources().openRawResource(R.raw.resume));

Related

Import PDF in my xamarin shared code application

I want to import pdf in my xamarin android project.
I want to have a pdf in my application folder at the installation of the application.
Bu when i try to add this pdf in android ressources and try to open it, i get a path error.
i save my pdf in draxable ressource and call path like : #drawable/pdfname or pdfname. the two solution don t work.
Best regards,
ok, i put it in the asset folder. I want to send email with attachement. I try this but the app said me the file is empty. var file = new Java.IO.File("Assets/CV_2017.pdf"); var uri = Android.Net.Uri.FromFile(file); i set my ressource as "build as android asset".
To get the url of the file in assets folder, you can code for example like this:
var file = new File(Android.Net.Uri.Parse("file:///android_asset/CV_2017.pdf").ToString());
if (file.Exists())
{
var uri = file.AbsolutePath;
}
If you want to access the stream of the file under assets, you can use AssetManager and code for example like this:
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader (assets.Open ("CV_2017.pdf")))
{
//TODO:
}

Unable to read file from the Downloads folder for an android app

I want to create an inputstream for a file that is stored in the Downloads folder of my phone.
Earlier I had the required file as an asset and I read it with this code:
InputStream fin;
fin = getAssets().open(FILENAME);
However now I want to be able to open a file stored in my downloads folder /mnt/sdcard/Download as an InputStream NOT as a FileInputStream Object. I am using a library which requires an InputStream only as an argument so I can not use the FileInputStream
methods because I get a fatal exception if I do and the App stops.
How do I go about this? I tried to find some helper function from the documentation, but could not.
Edit: I also tried editing the code to this-
String path = "/mnt/sdcard/Download"+FILENAME;
fin = new BufferedInputStream(new FileInputStream(path));
However still when I try to read the file, the App crashes.
I had the same issue as yours :
AssetManager am=getAssets();
InputStream is=am.open("file1.xls");
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Here I used to access file1.xls from the assets folder.
I fixed it by using :
File filepath = newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"file1.xls"); // fist part gets the directory of download folder and last part is your file name
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Don't forget to get the storage permission first to access the storage.

(Android) Read matlab file from assets

I use the jmatIO library to read a .mat file. In plain java I can set the path to the matFileReader like this
MatFileReader mfr = new MatFileReader("/theta-phi_small_param5.mat");
and I can have access to all the .mat data. Inside the android i put the .mat file to the assets folder and I tried to access it like this
mfr = new MatFileReader("file:///assets/theta-phi.mat");
but it doesn't work. How can I get the path to the mat file inside the assets folder so to read it with the MatFileReader?
Does the MatFileReader accept an InputStream? If so you can do it like this:
InputStream in = getAssets().open("theta-phi.mat");
It might also work to use:
File file = new File("file:///android_asset/theta-phi.mat");
UPDATE: Since MatFileReader doesn't support InputStream and the File solution above doesn't work I guess your best bet is to copy the file from the Assets folder to your apps External/Internal storage and from there access the file.

FileNotFoundException in an Android app: can't find the route to copy the file

Good afternoon everyone,
I'm having a problem here with an CSV file that I wanna use to fill a dynamic table in Android.
I placed the file in the "data" folder of my emulator, and I did the following declaration to create my file variable:
file = new File("/data/Motors.csv");
I used a CSVReader class posted on another thread from this page (Link to the class) and also declared the following line to do the parse:
readerCSV = new CSVReader(new FileReader(file),';','"',0);
In the last line, the programmer to be able to create the variable has to send as parameters a Reader (Or a FileReader, it doesn't make a difference), the separator char, the quote char and the number of how many lines the CSV reader has to skip.
The problem comes that no matter in which directory (Not even in the root directory, which Java tells me that it's "/" through the file.getAbsolutePath(); method) it always gets to the point when the program throws the FileNotFoundException and I'm getting a bit frustrated because I have 2 days already with this problem.
If someone could please help me to get a bit of orientation of what I should do to get the file from the right directory... It would be a lot of help and I can go further with the code I have to finish.
Many thanks in advance!!!!
EDIT
I found a solution to open my file:
I have used 2 file variables: one gets the rout of the External Storage Directory (see first answer in the following link) and the other is declared as a new file, like the following lines in the code:
route = Environment.getExternalStorageDirectory();
file = new File(route,"Motors.csv");
Later I used again the CSV reader class that I found, where the first parameter is from the FileReader type (Sorry #Rajesh, but I couldn't use your alternative because you passed a FileDescription parameter from the AssetManager to the CSV reader class and then I had to change the whole class and that didn't help my problem at all).
Now I don't get the exception at all!!
There are various Storage Options in Android. Files can be stored in internal and external storages. If the CSV file is a static resource, you could think of bundling the file in the "assets" folder of your project.
If you are using the "assets" route, the following code segment can get you a CSVReader.
AssetManager am = getAssets();
AssetFileDescriptor afd = am.openFd ("Motors.csv");
FileDescriptor fd = afd.getFileDescriptor();
readerCSV = new CSVReader(fd, ';','"',0);
The above code snippet doesn't perform error/exception handling, please remember to take care of that. You need to copy the Motors.csv file to the "assets" folder within the project.

How to read an xml file from R.java

I have an xml file inside the res folder under the xml forlder. I want to parse it with SAX. I know there are lots of example but I want to understand InputStream, InputSource, File deeply. I can reach the file writing R.xml.hippo (hippo is the name of the xml file).
But how can I give this resource as a stream?
I know when I write R.xml.hippo to anywhere it will be reference(int) but I want give it as a source. What is and how converting an int to starting point of stream?
You can read XML from assets folder as well as from res/raw folder.
To read from assets folder
AssetManager manager = getAssets();
InputStream inputStream = manager.open("your_xml.xml");
To read from raw folder
InputStream inputStream = getResources().openRawResource(R.raw.your_xml);
And parse this inputStream to your XML Parser.
You should place the file inside the raw folder. Then you can access it via Resources.openRawResource(). For more information refer to Android Developer's page.

Categories

Resources