I'm working on a feature that lets users select their own background image in an app.
Should that image be copied into an app associated folder after they selected it or what's convention for this?
In this case the user has freedom to select a picture from the other folder hierarchies and that picture can be deleted by the user. So you should save a copy of that image in your application folder and decode that picture to use as the background.
But the user can repeat this operation several times and saving a copy of each picture will consume a lot of space so, you should just create a single file that will be overwritten each time the user selects a picture to change the background.
And you need not to have a very high resolution image. You should down sample the image and then save it.
Follow this link for effective bitmap decoding.
Related
Lets say I have an App that allows me to rate mugs. Therefore, I enter some rating criterias, take a Picture of the mug and then save it to a DataBase, to look at it later.
At the moment, I save the picture on a path obtained by
getExternalFilesDir(Environment.DIRECTORY_PICTURES)
and save the path to my SQLite database to look at it later.
This works fine, however, as I rate a hole bunch of mugs all day and change my phone quiet often, I want to be able to backup my App and its data by common Android backup solutions. This works fine with the SQLite Database that holds the data and is stored in App-Context.
But since the database just holds a path to the taken picture (as returned by getExternalFilesDir), the picture is not backed-up. Where do I have to save the picure, to ensure that any common Android backup software will also grap the pictures?
Is it possible to ensure that the path stored in the database is the same, after I put the backup on a new phone? Since it may be possible that the App is located somewhere else on the new phone, absolute paths are not a good idea here... Is it possible to save the picture relative to the App and just save the relative path?
As suggested by greenapps, a possible solution is to save just the relative path of the image in the database. Instead of using
getExternalFilesDir(Environment.DIRECTORY_PICTURES)
I now use
getFilesDir()
to get the app intern folder and save images there. When I want to load the picture I build the path with the same method to get the app dir and the relative path saved in the database.
My Android app creates a folder on the users device's external storage on launch:
File images = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "rapical" + File.separator);
images.mkdirs();
I have around 16 image icons (.png) that I would like to place in this folder the first time the user opens the app.
What is the best practice to do this? Should I place them in the drawable folder initially and then copy them over to the newly created images folder? Not sure what approach to take!
There are two separate issues here:
What to do with images that the user adds to your app?
What to do with your starter images, for your default foods?
Using external storage for the user-added images has some implications:
Your app can survive that image no longer existing, since the user, or other apps, can delete that file at any point
Your user does not mind that the image will get picked up by other apps, such as photo galleries
Unless you specifically want these images to be user-manipulable outside of your app, I suggest that you use internal storage for the "re-sized, compressed and stored" user-supplied images. The original image might be on external storage (I assume that you are using ACTION_GET_CONTENT and/or ACTION_OPEN_DOCUMENT to get the image), but your modified copy would be private to your app.
I sincerely hope that you are using an image-loading library, like Picasso, for loading these images, since they will handle things like background threads and ListView/RecyclerView image recycling and stuff for you.
In that case, what you store in the SQLite database for your default foods needs to be something that the image-loading library can interpret, to bring in the image that you want.
In that case, I would suggest using assets/ to ship the images and file:///android_asset/... values in the database. file:///android_asset/ points to what amounts to assets/ in your project, so if you have assets/chicken_pad_thai.jpg in the project, Picasso (and any decent image-loading library) would be able to interpret file:///android_asset/chicken_pad_thai.jpg) and load the image.
This saves you from making duplicate copies of the images, saving the user disk space and time on first launch. It also means that if you replace the images in your app (e.g., you get a better photo representing chicken pad thai), the user will start seeing the updated image, without you having to do some extra work to realize that you shipped a new image and have to copy that image out to a file somewhere.
Now, suppose that you really do want the user-supplied images to be on external storage. In that case:
Probably rather than the directory that you chose, I would go with getExternalFilesDir() (a method on Context), as on API Level 19+, you do not need WRITE_EXTERNAL_STORAGE as a permission
If you want the user to be able to manipulate the images of the default foods, then copy those out to that location on first run
I have an application which downloads the image and saves in the sdcard of the device and meanwhile it saves the name of downloaded image in the database along with their path (this path is a physical path on device)
Why I am doing this? It has only one reason and that is not to download images again and again from the server. But as I am saving them in the sdcard then they are showing in the Gallery. So there is chance that user can delete some of the images or may be all of them. (Due to some reasons I am not saving them in app folder)
So what I want
I want couple of things that can be simplify as following
I want to see that if any of the image has been removed/deleted by the user, if so then I may download that also and then I will update my Grid view accordingly. If there is no deleted image then it should show normally.
I searched on internet and I found one method to check if the file is still there "myFile.exists()" but do not know how to use it in my case.
This is the worse point and could be hard of my logic, what Is going on now is I open my activity and it takes the image path from the database and fetch the image into the imageview. but what If there is sime 1 present , image 2 present but not image 3 , image 3 is deleted by user , then Now How to adjust this thing , as we are in the adapter of the gridview, how we would wait and download this image and would fit it at that place ?
Please tell me How to do this and How to check if any of the file has been deleted. ?
I need to manage dynamic pictures for users profile. Pictures will be created or deleted in runtime by users. Where should I locate this pictures (drawable folder, specific folder...)?
If you think the best way is to use a specific folder, how could I get an image from this folder to be displayed it in an ImageView. I know how to do it from drawable folder, but I do not know how to do it from a specific folder.
Finally, users will select pictures from phone gallery in runtime. I want to copy this picture from phone gallery to a folder (drawable or specific) for future use.
None of the above. They should be located on the SD card, which is right where the camera will put them. When the user selects an image via the gallery or takes one via the camera from your app, you'll get a URI for the file. Save the URI somewhere and use that to display it later.
I want to do this. my app has many screens and in each of them, on the top, I display an imageView as a logo. So I have 20 screens and that means (20*3) images in my drawable which makes my app be many Mb's. Because this image is static and never changes I want to do this: Getting it from the web (I know how to do it, I am not asking this) only for the first time this screen is ever launched, then this image be stored somewhere in user's device and then use that path as a source. (I mean not download it again, because it will be annoying for the user waiting every time). So is it possible? Will it make my app go slower (not the first time, but the rest) because I am retrieving data from SD?
Yes, you can do this fairly easily. You also do not have to store the data on the SD card necessarily. You can store the image in the internal storage.
Basically, set up a cache directory. When you need the image, check the cache directory, and if the file is not there, download it over http and store the file in the cache directory.
It will change how you get the resource (e.g., you won't be able to use R.drawable.imagename), but you can just load the drawable programmatically.