I'm developing an application for android, the application shows a list of activities of a carnival. I want that the application has the images of every activiy to avoid the user has to download it when runs the application, and improve user experience's with the application.
To do it I have added the images for every activity as resource, but I don't know if this is the best way to do It. There are some limitation with Resource that I don't like for example you can't create subfolders in drawable folder to organize the images, the only characters accept as a resource name are [a-zA-Z0-9_], and others....
Is there any good way to do it? or I have to use resources if I want to avoid the user has to download the images.
Thanks
You can download images when user load your application first time then save it to sdcard.
When your application runs again check weather images available in sdcard or not.
If not then download images else use images from sdcard.
The android system is designed to have all of your drawable items (aka images) in the resources folder. If you want the images to be packaged with your app (rather than downloaded after the fact) they need to be included in res/drawable
Its always better to have the images downloaded but if your requirement is that the application should have all the pictures beforehand then you can use the "assets" folder where I suppose there is no restriction with respect to the names of the pics. I have tried the following code and it works:
ImageView img = (ImageView)findViewById(R.id.activity_image);
try {
Drawable d = Drawable.createFromStream(getAssets().open("#ahd ka.png"), null);
img.setImageDrawable(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have purposely used a png file with special characters in name just to show that the program runs. Hope this helps.
Related
I have searched for a few hours now and can't seem to find an answer to my question(s). I have written the following lines of code in the android ndk(c++) and I am using the needed opencv libraries to accomplish the task.
void opening_images(){
Mat image ;
sillyString = "I have changed";
String imagePath = "//drawable//ring.png";
image = imread(imagePath,IMREAD_COLOR);
if(image.empty()){
sillyString = "Image not loaded";
}
else {
sillyString = "Image loaded";
}
}
I have tested this code in Qt with opencv and it works fine.At the moment the program in android studio returns the "Image not loaded" string. I think the main problem which is present is, the fact that I don't completely understand how to work with the file paths? In android studio I have included a picture under res/drawable/ring.png. I am able to view this image using the java side of the app.
Question 1: Is the specified imagePath = "//drawable//ring.png" correct to access the ring.png file ?
Question 2: Is there any permissions needed allowing the ndk to access res folders ?
Question 3: Is there any similar methods to assign an image to a Mat object?
Any help will be appreciated.
Edit:
If you take a look at how BitmapFactory decode resource works - you will see that getting bitmap from drawable still requires unpacking of a compressed image.
So answer to your q1: No it is not correct way to access ring.png, you will either have to download resource to your device or unpack it to byteArray and use imdecode instead of imread
Since this was my first time using Android Studio there was much to learn. It took me a while but here is the answers to the questions that I posted.
Question 1: Is the specified imagePath = "//drawable//ring.png" correct to access the ring.png file ?
This is most definitely not the correct path to use when accessing images for the purpose of image processing etc.The drawable folder can still be used to update ex. an image view by setting the src of the image view to the image
imageView.setImageResource(R.drawable.ring);
When working with images and Mat objects, I found it best to use the Android debugging bridge to copy the files to the SD card of the device. This link will provided you with the necessary steps to install the adb https://www.howtogeek.com/125769/how-to-install-and-use-abd-the-android-debug-bridge-utility/
When the images are done copying to the SD card. The file path can be found by making use of the built in Java functionEnvironment.getExternalStorageDirectory(). Which looks something like /storage/emulated/0/YOUR_file it depends on the location which was used to copy the files to.
Useful tip: Download ES File Explorer for the device to help navigate through the external or internal storage.
Question 2: Is there any permissions needed allowing the ndk to access res folders ?
The method which I used didn't need any permissions. However at the moment the NDK side cannot directly read an image from the SD card, the image must be passed from the Java side by making use of assets or by passing the address of the image which was read into the Mat object(Java side).
Read and write permission is needed in order to access the SD card. This must be set in the manifest.xml and must be correctly implemented in the code. There are many great tutorials on YouTube.
Question 3: Is there any similar methods to assign an image to a Mat object?
This question seems redundant now, there are many ways to skin a cat.
In short, I think it is easier to stick to the Java side when using Opencv4Android and some form of image processing is needed.To get you started in java here is a small snippet from my code.
Mat image;
String imageInSD = Environment.getExternalStorageDirectory()+"/Pictures/Data/Birds/"(ImageFolders[i])+"/"+String.valueOf(id)+".png";
image = Imgcodecs.imread(imageInSD,Imgcodecs.IMREAD_COLOR);
Good luck!!
Another way to use saved image in NDK is follows,
Then drawable folder, you can save it in assets folder. This helps to access multiple images also easily.
Then BitmapFactory.decodeStream helps to take it as bitmap and Utils.bitmapToMat is used to convert bitmap image to Mat file.
Then this Mat file, you can pass to NDK and process it using OpenCV C++.
Thanks
I have an application which has loads of pictures. The total picture is saved in an SD card due to large number of picture. The main problem is that the pictures are available in gallery. I want a way to hide the picture from the gallery but must be available for my application.
I did put " . " in front of the file name to make it hidden
but the images were not displayed in may application also!
Anyone.. help.. please
thanks..
Gallery handles all media files it knows about, so if you want it to stay away of your assets you got two solutions. First, add .nomedia file to the folder with your images, and Gallery should skip it (most image viewers would honor .nomedia, yet there's no guarantee). Other solution is to use some own, proprietary file format, so Gallery or other tools won't be able to process it. I'd recomment .nomedia if that's sufficient solution.
Add .nomedia file into the image folder. The important thing is that, the folder should be empty, when you add .nomedia file. If the folder contains some images before adding '.nomedia' file, the gallery will not hide those images, and it will hide the images added after '.nomedia' file. So The first file added to the image folder should be '.nomedia'.
Hope dis will help you
Thankx,
This line of code can add the .nomedia file in your directory and makes your images hidden from the gallery
String NOMEDIA=" .nomedia";
File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
if(Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
if(!nomediaFile.exists()){
nomediaFile.createNewFile();
}
}
i think the place where you save the images matter, try to save them into application cache directory, or files directory.
you can get access to cache directory using : getCacheDir() in your activity.
and your files directory: getFilesDir()
these two directories must not be visible or accessible to any other applications expect your application,
I used assets to accomplish this in my app.
I have a bunch of images that are common for all resolutions (currently I only support one resolution. There are reasons for this).
My question is, when is it appropriate to store images in the assets folder rather than the drawable folder?
The difference between a raw resource in res/raw directory and an asset is that assets behave like a file system, they can be listed, iterated over, discovered, just like files while for raw resources, you need the resource ID.
From: http://mylifewithandroid.blogspot.it/2009/06/assets.html (emphasis mine)
Also you may use assets when you need to organize things using sub-folders.
you can change the files in assets folder anytime you want and nothing will be effected (i mean the resources of your application by "nothing"). it may be useful to give the name of the image file as a parameter for example.
If you want to access images by their original name, you better save images in the assets folder. Then, you can get them by using:
AssetManager:AssetManager am = getResources().getAssets();
try {
InputStream is = am.open("image.png");
// use the input stream as you need
} catch (IOException e) {
e.printStackTrace();
}
I Hope this helps.
I have images placed in following path
res>drawable>Images>Currencies>[All images]
I have made a custom spinner Adapter.
viewHolder.flag = (ImageView) view.findViewById(R.id.UICurrencyCurrencyFlag);
viewHolder.flag.setImageDrawable(drawable);
How can i set these images in my ImageView. Images format is png
Best Regards
You have 2 choices:
Put all images in drawable folders without subfolders.
Put all images in asset with your folder structures.
The differences are:
If you go for approach 1, you can retrieve the resource by id. (R.drawable.*)
If you go for approach 2, you get to maintain your folders, but you need to read the resource as raw.
To open file in asset folder, you can refer to a previous post:
Opening a File from assets folder in android
Suggestion:
Rename your file as images_currency_image1.jpg to avoid subfolders but keep unique-ness.
I am not sure if this help in your situation.
You don't really need to make subfolders inside drawable folder. And even if you do, you can't access the contents inside it. And it doesn't even make sense. Why you need subfolders when android is taking care of giving you the drawables by their id. You place your images directly into res/drawable folder like res/drawable/[All images]. And then try to access by R.drawable.UICurrencyCurrencyFlag and not R.id.UICurrencyCurrencyFlag. All the best.
if u place in assets folder u can access in following manner
InputStream bitmap;
try {
bitmap = getAssets().open("icon.png");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
ImageView img = (ImageView) findViewById(R.id.myId);
img.setImageBitmap(bit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So I'm having the user create a profile, which includes an avatar. I would like the program to, like the user's name and other info, remember the avatar so it appears as their avatar whenever they use the app. I'd prefer for this bitmap to be saved when created, so the app doesn't need to rebuild the avatar (which is scaled and whatnot) each time the app is started. Is this possible? From the looks of it this wouldn't be able to be saved in SharedPrefs... any idea what the best way to do this would be? Thanks.
Followup: Followed CommonWares's suggestion. Saved to SD... but having trouble calling it back when the app is reloaded.
Currently, in onCreate:
String path = "testapp/images/thumbs/"+m_username+".jpg";
File file = new File("testapp/images/thumbs/"+m_username+".jpg");
if(file.exists()) {
Log.e("TRY TO GET THUMB", "I EXIST");
m_thumb = BitmapFactory.decodeFile(path);
Drawable draw = new BitmapDrawable(getResources(), m_thumb);
m_photoButtonE.setBackgroundDrawable(draw);
}
Does not find the file, says it does not exist, though when I check my SD card the image is in that exact spot, with the proper file name and everything. Any ideas? Thanks all.
Is this possible?
Save it in a file.
From the looks of it this wouldn't be able to be saved in SharedPrefs... any idea what the best way to do this would be?
Save it in a file. If you intend on having multiple avatar files, put a path to the saved avatar file in the SharedPreferences.