Encode images
The first solution I came up with is to encode the image for a string in Base64, thus writing the same in a file:
Function that receives image and returns Base64
public static String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
Log.e("LOOK", imageEncoded);
return imageEncoded;
}
Function that receives Base64 and returns the image
public static Bitmap decodeBase64(String input)
{
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
With this solution, when you save the images, you encode them in Base64 and save them in a file, for example nomeDaImagem.codedImg
.
When you need to present them, you find the string file read and display the image.
User response credits @Romantruba in this answer in the SOEN.
Imagens Privadas
If the idea is that the files can only be accessed by the application in question, without any other being able to handle them, the solution is to have those files in the Internal Storage where they are private and accessible only by User ID of the system assigned to the application at the time of its installation.
For this scenario it is unnecessary to encode and decode the files since they can only be accessed by the application that created them.
Modify the image to make it appear corrupt to other programs. (you can place a sequence of
bytes
pre-defined before saving)– Mansueli