Reputation: 316
I'm currently working on a dashcam app and I was trying to store images over a specific timeframe and either delete them when they are too old or create a video from them.
However I am constantly running into a storage problem. My plan was to cache the bitmaps in the device memory, because worstcase would be 60 images per second and compressing bitmaps takes some time, so I wanted to do that only if I have to. Like when generating the video out of single images.
I wanted to preserve the lifetime of the NAND-Flash storage. So my plan was to save a list of bitmaps in system memory and only collect, compress and save them to storage when I want to generate the video.
So tl;dr: Is there a way to save ~1GB of temporary data on android except writing and deleting it to/from storage?
So I already tried some different things:
Saving it to storage (first just for debugging but after finding no solution to my problem I still use this part)
I also did some research about saving to RAM in android. It seems that you cant store more than 1MB which is far to less to even try it. https://developer.android.com/guide/topics/data/data-storage.html?hl=de
I also thought about using bitmap recycling. https://developer.android.com/topic/performance/graphics/manage-memory#java However I dont understand this piece of code good enough to even guess if this might work for me.
//This is what I'm using at the moment
private void storeImage(Bitmap bitmap){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/savedImages");
directory = myDir;
image_save_path_test = root + "savedImages";
Log.d("Directory",image_save_path_test);
if(!myDir.exists())
{
myDir.mkdirs();
}
Random rng = new Random();
int n = 10000;
n = rng.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if(file.exists())
{
file.delete();
}
try
{
FileOutputStream out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}}
Well I expected Android to delete the temporary files because thats what written in the documentation. But thats also why I'm asking for help so I'm not sure what to say here other than thanks in advance.
Upvotes: 2
Views: 180
Reputation: 721
I wanted to preserve the lifetime of the NAND-Flash storage. So my plan was to save a list of bitmaps in system memory and only collect, compress and save them to storage when I want to generate the video.
This right here is the heart of your issue: you don't need to do that at all. Modern flash have a large number of erase-cycles (100,000+), on top of that if you add wear-leveling and aggressive caching by the OS, it means you are thinking about a non issue: save large files to filesystem and don't bother reinventing this old complicated and perfectly functioning wheel.
collect, compress and save them to storage when I want to generate the video
I think by directly using the compressed video feed from the camera instead of compressing raw BMPs by yourself, you will be much more efficient. Seriously MPEG compression is better than anything you will come up with and the cost is nil because nowadays embedded cameras have built-in stream encoders that start working as soon as the camera is turned-on.
I would focus more on the specific of the BMP and video I want to generate and analyze / distribute (resolution, GOP-size...) in regard to phones on the market, rather than the specifics of how to compress BMP or how to protect the NAND.
Upvotes: 1