File read only by the Android Java application

3

I'm doing an application that reads images from a folder, but I have a problem, I need to prevent other applications (eg gallery) and even file manager from being able to open this image ...

How can I make only my application read (write, delete) the images in a directory? This is on Android.

Any examples?

    
asked by anonymous 26.06.2014 / 07:06

3 answers

0

I solved the problem!

I followed Zuul's idea, but one thing was missing ... What I did:

I will not put the whole code here, because it's huge, but the logic remains.

  • First I created a hidden folder (.nomedapasta) in the sdcard and I added the .nomedia file in the folder.

    // Use to return the sdcard dir

public static String sdCardPath = Environment.getExternalStorageDirectory (). getPath ();

public boolean criarDiretorio(String path){
    java.io.File dir = new java.io.File(path);
    if (!dir.exists()) {
        dir.mkdirs();
        return true;
    }else {
        return false;
    }
}
  • Captured image names and rename them all using AES encryption (including extension!)
  • When I need to read the image, I run it with the descripe name!

Details:

  • The .nomedia file prevents the gallery application from reading the images and videos in the folder!

  • Encryption uses special characters, and can not rename the file using these characters (ex: +, =, /), so I override these characters before renaming and resharpened when decrypting. For example, Replace / by -, when I decrypted I changed - to /. (if someone does the same, do not replace the special characters of the cryptography by letters, look for some character that does not use, such as _ or -).

The last solution seems a bit tricky, but that's what I managed to think, it's working.

EDIT: Can be used = and + in the file name, in this case only / should be changed. It is not necessary to encrypt the file name, only in my case it was necessary, because (I did not mention about why I already knew how to do this) I need only the app to know the file name. So I recommend that you only rename the file by adding "an extra extension", for example: fotosdaviagem.png >

    
26.06.2014 / 23:51
8

Encode images

The first solution that occurred to me is to encode the image to 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 to a file, for example nomeDaImagem.codedImg .

When you need to present them, you decode the string read from the file and present the image.

User Response Credentials @RomanTruba this answer in SOEN.

Private Images

If the idea is that the files can only be accessed by the application in question, and no other can move them, the solution is to have those files in Internal Storage where they are private and accessible only by the User ID of the system that was assigned to the application when it was installed.

For this scenario it is unnecessary to encode and decode the files as they can only be accessed by the application that created them.

    
26.06.2014 / 11:49
0

Basically you can put a nonexistent extension to the files ... if you do not want anyone to open and see what it is ...

Put a few different names too:

523626.jpg = > 523626.dat

When your application is read use:

BitmapFactory.decodeFile(523626.dat);

That way, only your application will know what each file actually is. (for images)

  

Remembering: If you want applications such as Gallery and others to "see" your files, create a .nomedia file in the directory. Therefore, these files will not be indexed.

    
28.06.2014 / 03:42