How to save/recover image in memory on android?

Asked

Viewed 2,322 times

7

Hi, I have a Array of Bytes which is a image, from it, how can I save in the external memory and if there is no save in the internal memory android? and then how can I be recovering this image?

I need to save in a place that these images do not appear in the mobile gallery, only in the application.

  • Create a table in the sqlite database and save the byte array as a Blob object. http://developer.android.com/training/basics/data-storage/databases.html http://developer.android.com/reference/java/sql/Blob.html

  • But that’s what I’m doing at the moment, but the images with a size of 2MB are giving error when it comes to reading. I think if I persist in BD images with this size can get mt heavy the sqlite.

2 answers

4


Let’s first write a method that checks the existence of Sdcard and whether it is possible to write on it:

public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

The method returns true whether it is possible to write on Sdcard or false if it does not exist or for any reason it is not possible to write on it.

We need two methods to save the byte[], one to save to internal memory and one to save to Sdcard.

Save to internal memory:

public void saveArrayToInternalStorage(String fileName, byte[] imagem){
    try{
        FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
        fos.write(imagem);
        fos.close();
    }catch (IOException e) {
        Log.w("InternalStorage", "Error writing", e);
    }
}

Save to Sdcard:

So that the Mediastore do not see the images we will use getExternalFilesDir() to obtain the Path to Sdcard.

public void saveArrayToSDCard(String fileName, bytes[] imagem){
    File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File file = new File(path, fileName);
    try{
        OutputStream os = new FileOutputStream(file);
        os.write(imagem);
        os.close()
    } catch (IOException e) {
        Log.w("ExternalStorage", "Error writing", e);
    }
}

How to use:

if(isExternalStorageWritable(){
   saveArrayToSDCard("nomeDaImagem", imagemEmBytes);
}else{
    saveArrayToInternalStorage("nomeDaImagem", imagemEmBytes);
}  

If the application is to run in versions prior to Android 4.4, it is necessary to get permission WRITE_EXTERNAL_STORAGE, add the following to Manifest

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                     android:maxSdkVersion="18" />
    ...
</manifest>  

Note: Images saved by these methods will be deleted when the application is uninstalled.

Source: Android Storage Options

  • Thank you very much, I’ll be there tomorrow. and refer to note "Images saved by these methods will be deleted when the application is uninstalled. " is a great relief, because the images I’m working on are a bit "heavy".

1

From what I read in that topic of Soen the limit is 1 MB, more than that you would have to use the NDK.

In this same topic there is also another approach that uses the sqlite4java.

Take a look, it might be useful.

  • Very good to know that the error occurs because it exceeded the established limit, I was initially worried that it was some error in the code. Interesting the sqlite4java, I will study the chance to put it in this my project, but in the future surely I can be using in another project. Thank you Bruno

Browser other questions tagged

You are not signed in. Login or sign up in order to post.