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.
Related
The actual code is very simple like this but doesnt works!!
File f = new File("file://E:/test.jpeg");
Picasso.with(this).load(f).
into(avatar);
File f = new File("file://E:/test.jpeg");
First, Android does not have drive letters, let alone an E: drive. That is not a valid filesystem path to any file on any Android device.
Second, the File constructor takes a filesystem path, not a Uri with a scheme (e.g., file://).
Its important to know that the file you are accessing has to be on your DEVICE and NOT from your computer. There is no such directory as a E: drive on phones.
You have a few options. Store the image online, and load it with Picasso (the easiest).
Picasso.with(getActivity())
.load("http://www.image_url.com/image.png")
.into(avatar);
Or, you can get the file path of an image, and then use that with picasso.
File file = new File("path-to-image/image.png")
Picasso.with(getActivity()).load(file).into(avatar);
How do you get the path-to-image of an image on your device?
You can follow this http://www.limbaniandroid.com/2014/03/how-to-get-absolute-path-when-select.html
My app depends on 3 different files. I already put the two larger ones as expansion files which seems to work fine. Now I don't know where to put the third one (small file). I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
I tried this here for the raw folder
myClassifier.loadModel( pathToExpansionFiles + "/File1",
pathToExpansionFiles + "/File2",
R.raw.File3);
However the return value of R.raw.File3 is an integer, but the function myClassifier expects a string that is the path to File3. Has anyone an idea how to do that?
I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
There is no path to it. That is a file on your development machine. It is not a file on the device. It is merely an entry inside your APK file.
but the function myClassifier expects a string that is the path to File3
Either switch to some library that works with an InputStream (from getResources().openRawResource()) or make a copy of the data to a local file using that InputStream yourself.
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.
So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];
I need to get Absolute path to Folder in Assets.
Some like this for sd-card:
final String sdDir = Environment.getExternalStorageDirectory() + "Files";
What i do incorrect?
First I try to get path (in green ractangle) this way but I alwase get "False".
Then I comment this block and try to get path from getAssets().list();
But I get 3 folders witch I see first time.
I want to make massive like this "green" but I need to use files from assets:
Look the image
Help me to get absolute path to my Files folder.
I'm not exactly sure what you're trying to do, so I'll try to cover the bases, and maybe this will help.
If you are just trying to get a list of what's in your assets, then
use getAssets().list("Files"). (You have to use the subdirectory,
because of this).
If you are trying to get an absolute path to your assets directory
(or any subdirectory), you can't. Whatever is in the assets directory
is in the APK. It's not in external storage like on an SD card.
If you are trying to open up files in the assets directory, use
AssetManager.open(filename) to get the InputStream. Here,
filename should be the relative path from the assets directory.
EDIT
I'm not sure what you mean by "massive", but if you want to load the file black.png from assets instead of the SD card, then write this:
// must be called from Activity method, such as onCreate()
AssetManager assetMgr = this.getAssets();
mColors = new Bitmap[] {
BitmapFactory.decodeStream(assetMgr.open("black.png"));
// and the rest
};
Assets are stored in the APK file so there are no absolute path than your application can use. But, I would suggest to take a look at file:///android_asset. It might fits your needs. Here is a good example on how to display an Asset in a WebView.