Open an image from asset folder in android - android

I need to get the images from asset folder by specifying the name of the image and set it to Drawable.
drw = Drawable.createFromStream(getAssets().open("/assets/images/"+drawables[i]),null);
But I get File not found exception.(images is subfolder of assets and drawables[i]- name of the image(say "ball.jpg").

Write instead:
drw = Drawable.createFromStream(getAssets().open("images/"+drawables[i]),null);
file path should be:
"images/"+drawables[i]
when you write getAssets() means open assets folder and find out path there.

Related

Is file accessing process from asset folder the same as, accessing a file from external storage

I have a method in my code which takes path of a folder as an argument. When I am using SD card I can simply give the path, but can I do the same with asset, and if so how? I need to write File f=new File(path) so that I can give f.getabsoultepath() as my argument. so what should be the value of path lets assume this is how my asset folder is orginzed and arc is my folder

Loading a file from res folder in android test project

In a testcase I need to load a picture which is available in my testproject in the folder
/myproject/res/drawable-hdpi/attachment.png
I tried it already by using the InstrumentationTestCase and storing the file in the /res/raw folder and loading it with this, but the R file does not contain my attachment.png
Context testContext = getInstrumentation().getContext();
InputStream input = testContext.getResources().openRawResource(R.raw.?????????)
any idea?
thanks
You should not use raw to save images, used drawable o asset ;)
Difference between /res and /assets directories
Android images in /assets or res/raw

What is the path directory to res/drawable type String to pass through File(String path) in Android?

I am trying to get String path directory to images in the res/drawable and filter through it (jpg, png ..)
Something like this
File folder = new File("/res/drawable");
images = folder.listFiles(IMAGE_FILTER);
File image = images[randGen.nextInt(images.length)];
From sd card works easily but couldn't make it from the resource folder! I don't know if this is naive question but I have been trying different options like passing through uri, casting bitmap to File, no luck!
I am trying to get String path directory to images in the res/drawable
Resources are not files on the device. They are entries in the APK's ZIP file. Hence, you cannot construct a File that points to a resource.
You can use AssetManager to list assets, open InputStreams on assets, etc.

Referencing files in Android

Why can't I refer to a PDF file in my Android device?
I have a folder named "templates" in the root folder, and I have a PDF file in that. I've tried to refer to the file via the following:
File file = new File("/templates/myPDFFile.pdf");
but when I try to print file.exists(), it always returns me false.
Just as further information, my file hierarchy is:
root
-src
-com.example.text
-MyActivity.java
-templates
-MyPDFFile.pdf
and my code is stored in MyActivity.java.
How do I reference accurately to the PDF file?
You must put it in assets and get it from there like :
How to open a pdf stored either in res/raw or assets folder?

Android: get route of file

I need the route of a pdf file in my Android Project.
The file could be in raw or in assets folder.
I need to get the route because I must call using the File Class
Ex: File file = new File(path);
Thanks
Image:
http://img19.imageshack.us/img19/2303/capturaww.png
I am not quite sure what you mean, assuming you want to read to PDF file in your assets (res file) look here
Access PDF files from 'res/raw' or assets folder programmatically to parse with given methods
But if you want to get the file path of a folder, then look here
how to get file path from sd card in android
How to get the file path from URI?
Here is an example of how to get a file path (taken from the third link)
File myFile = new File(uri.toString());
myFile.getAbsolutePath()

Categories

Resources