I have a trouble with load image file in android project. I need to load from it because I have some private reasons.
here is my android project structure:
ProjectAA
----assets
----image
--------ic_next.png
----src
--------HomeActivity.java
----AndroidManifest.xml
I tried to load ic_next.png in HomeActivity.class with:
InputStream is = getClass().getClassLoader().getResourceAsStream("image/ic_next.png");
Bitmap bm = BitmapFactory.decodeStream(is);
but bm always return null. I also add "image" folder to java build path
I searched google but I can't find any solution. please help me.
Thanks
Put image folder under src folder or include image folder as source folder in project settings.
Related
i am downloaded and saved images from web to my app cache folder. Now i am trying to convert particular cache folder image(PREM.jpg) into bitmap. For that i am using bellow code.
FileCache file = new FileCache(Context);
File f = file.getFile("PREM.jpg");
Bitmap bitmap = decodeFile(f);
System.out.println("Exist status. " + f.exists());
But i am always getting file not exist (Exist status. false).
Actually "PREM.jpg" file name showing some thing like "-1006685176" in the cache folder.
This is my cache filder
Can you please help me how to i convert that particular image into bitmap.
Thank you everyone.
Prem
I try to use OpenNLP in an Android application with eclipse, and I imported the 4 JARs into the libs folder.
It's probably a stupid question.. but where should I put the model files "en-pos-maxent.bin"? I can't find anything regarding the path anywhere
I try to run the code contains this line:
POSModel model = new POSModelLoader()
.load(new File("en-pos-maxent.bin"));
I tried putting the en-pos-maxent.bin inside a new folder within the project("tagger/en-pos-maxent.bin"), inside the libs folder, or simply give the path where the en-pos-maxent.bin is put when downloaded("Users/ariel/Downloads/en-pos-maxent.bin"), it always give me the error: POS Tagger model file does not exist path: (the path I typed in)
Could anybody help please?
When you use new File("filename") the file is expected to be in the current working directory, I don't think that's different on Android. You can use System.getProperty("user.dir") to get the current working directory. It's by default the directory you start the program from. You could also specify a full path like new File("/full/path/filename") instead.
In android the right way to add file to the application (inside APK) is to put into the assets folder. Then you can't get File object from assets folder but, you can create that File using a buffer or in you case just get it into the InputStream. For example if you will create models folder into the assets folder (assets/models), your code will look like this:
AssetManager am = getAssets();
InputStream inputStream = am.open("models/en-pos-maxent.bin");
POSModel posModel = new POSModel(inputStream);
Im trying to create gallery application. In tutorial I've found, I need to do get images using R.drawable.image_name. I've created folder drawable under /res folder, copied my images one by one into that folder, al of them are .png. But when I try to access them by writing R.drawable.image I can't do that because there is no images I've copied. How to fix it?
Clear and rebuild your project. If you got automatic builds disabled, your IDE will not refresh R class contents and won't autocomplete your assets IDs.
Do clean build or if you are using android studio do invalidate cache and make project afterwards.
For Accessing Drawable:
Drawable drawable = this.getResources().getDrawable(R.drawable.image_name);
If you want bitmap :
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.image_name);
if you want to access by url :
imgUrl= "drawable://" + R.drawable.image_name;
Actually i know how to open PNG files as bitmaps. But my code doens't works for open JPG files, i dont know why.
I can't find correct examples on SO or google about how to do this.
I need to have a bitmap with the JPG file opened from a dir of the sdcard. For example "sdcard/images/01.jpg"
Thanks
File root = Environment.getExternalStorageDirectory();
ImageView IV = (ImageView) findViewById(R.id."image view");
Bitmap bMap = BitmapFactory.decodeFile(root+"/images/01.jpg");
IV.setImageBitmap(bMap);
Always try to use Environment.getExternalStorageDirectory(); instead of sdcard.
You need an ImageView somewhere in your layout, however that's how I do this kind of things.
I use this code personally too, and it works here.
Any of the BitmapFactory.decode* methods should be able to handle standard JPG files.
If you post some code it could be easier to see why it won't work.
I have an Android App which contains an external dependend jar library. This external library contains some png-bitmaps which I want to show on some ImageViews in my application.
Looking inside the apk file of my application I can find the images in a sub directory ("aq\img") as expected.
What is the best way to show an image (insight the classpath) on a ImageView.
I tried this
ImageView imgView01 = (ImageView) findViewById(R.id.imageView1);
Drawable d = Drawable.createFromPath("aq/img/sample.png");
imgView01.setImageDrawable(d);
and this
InputStream is = ClassLoader
.getSystemResourceAsStream("sample.png");
Bitmap bm = BitmapFactory.decodeStream(is);
imgView01.setImageBitmap(bm);
but it did not work (I did not really expect it to be that simple).
The image remains empty.
Thanks for help!
Regards Klaus
The PNG image is not a "system resource" because a system resource on Android is a file in the regular filesystem. The image that you want is within the APK archive. You would be able to use getSystemResourceAsStream to open an InputStream for reading the bytes of the APK archive, but not a file within the archive unless you implemented the logic to extract the file yourself.
Simply use this:
InputStream is = getClass().getClassLoader().getResourceAsStream("aq/img/sample.png");
EDIT: Note that on Android, the name that is passed to getSystemResourceAsStream is treated as a path to the file relative to root (/). Because the APKs are stored in /data/app, then to open an InputStream for reading the APK, you can use ClassLoader.getSystemResourceAsStream("data/app/PACKAGE-1.apk"), where "PACKAGE" is the package of your app.