Read file only by Java Android application

Asked

Viewed 528 times

3

I’m making an application that reads images from a folder, but I have a problem, I need to prevent other applications (ex: gallery) and even file manager can open this image...

How do I get only my application to read (write, delete) images from a directory? This on Android.

Some example?

  • 2

    Modify the image to make it appear corrupt to other programs. (you can place a sequence of bytes pre-defined before saving)

3 answers

8

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.

  • I personally don’t believe that Base64 encoding/Decoding provides guarantees that the file cannot be read, @Zuul - even most modern browsers can do this automatically via Data Uris - data:<MIME-type>;charset=<encoding>;Base64,<data>

  • @Onosendai I do not know the intent of the OP, I only know what he asked. On an Android device so that an image is not viewed in the Gallery if the file is not an image and has any encoding, we have solved :) Of course, if the intention is to protect the file against "all-or-something"then the solution goes through encryption and non-coding.

  • @Zull I understand your point, and I agree in principle - but the OP specified 'other applications (including Gallery)' - I imagine a browser fits the definition too, no?

  • @Onosendai Your analysis of the question makes sense! I updated the answer :)

0


I solved the problem!

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

I’m not gonna put the whole code in here 'cause it’s huge, but there’s logic.

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

    //Use to return right from sdcard

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;
    }
}
  • I captured the image names and renamed them all using AES encryption (including the extension!)
  • When I need to read the image I run it with the descriptive name!

Details:

  • The File. name prevents the gallery application from reading the images and videos from the Folder!

  • Cryptography uses special characters, and there is no way to rename the file using these characters (e.g.: +, =, /), so I replace these characters before renaming and swapping again when decrypting. For example, Replace / by -, when decrypting I switched - to /. (if someone does the same, do not replace the special characters of the encryption with letters, look for some character that it does not use, for example _ or -).

The last solution seems a bit tricky, but that’s what I thought, it’s working.

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

0

Basically you can put a non-existent extension to the files... if you don’t want anyone to open it and see what it is...

Put some different names too:

523626.jpg => 523626.dat

When your application is reading use:

BitmapFactory.decodeFile(523626.dat);

This way, only your application will know what is actually each file. (for the case of images)

Remembering: If you want apps like Gallery and others to "see" your files, create a. filename in the directory. Thus, these files will not be indexed.

Browser other questions tagged

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