In the process of writing an application for android, I had the task to save an arbitrary image to a file on a flash drive. In this article I will describe how I solved this problem, what difficulties I encountered in the process, and how they were solved. I would like to separately note that I am a .NET programmer, fate brought me to the Java world only because of the need to create small in size (therefore the monodroid is not immediately) and quite simple in terms of the interface of android applications. This means that I also just learn, and therefore I will be happy with any advice and comments from professionals.
So, suppose that we have an ImageView, which contains the image we need as a file. The first question is where to save this picture?
The most correct and approved approach by Google will save the image to a cache folder dedicated to your application.
You can get the absolute path to it by calling the function:
')
context.getCacheDir();
This folder is cleared when the application is uninstalled, besides its size is monitored by the system, and if there is not enough space in the internal memory, the files from there will be automatically deleted by the android.
There is a solution and disadvantages. The most important - the application cache folder is contained in the internal memory of the device, which is always lacking. It is recommended to save only temporary and not very large files there, saving a large number of heavy images is undesirable here.
It is better to use the application cache folder on the SD card, the path to which can be obtained by the function:
context.getExternalCacheDir();
This folder will also be cleared when the application is uninstalled, but its size is not monitored by the system, so before saving files there, it is advisable to check its status with the command:
Environment.getExternalStorageState();
In my case, due to a combination of different factors, both standard solutions seemed to be suboptimal, so I just save the files in the root of the SD card, the path there can be obtained by the function:
Environment.getExternalStorageDirectory();
Now about the saving procedure itself, its code is given below.
ImageView iv;
A few explanations on the code.
File file = new File(folderToSave, Integer.toString(time.year) + Integer.toString(time.month+1) + Integer.toString(time.monthDay) + Integer.toString(time.hour) + Integer.toString(time.minute) + Integer.toString(time.second) +".jpg");
Not very elegant, but simple as a pencil and really working solution to get a unique name for a file in a convenient sorting format - “2011 11 17 20 31 49.jpg”
There is only one little trick. Notice
time.month + 1 ?
The fact is that Java considers the months from scratch, and November is 10m, not 11m, as everyone is used to, the month. Disorder.
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
Here, too, everything is self-evident, disputes can arise only about the percentage of compression. It seems to me that 85% is the best option, but everyone can have their own opinion and their own conditions of the problem.
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
An interesting trick, which many people forget. Not all android users are advanced enough to find the files you have saved, even if you showed them the file system path. But here about the application "Photo Album" they know exactly everything (this is where you can see photos that you just shot). Add the above line to your code, and the picture will not only be saved in the correct directory, but also registered in the photo album, and the user will always find it without any problems.
By the way, the above function is taken from a real running Random Pictures application, which shows random pictures from the Internet, and in the paid version of the program - it can save any picture to itself on a USB flash drive.
Free version of Random Picture Free on the android market
Random Pictures Free
The described file saving function works only in the paid version. Its price is 45 rubles, but for the masters, of course, it is laid out for free. (A good tradition, I must say!) Link from the author in the comments.