0

I want to hide the snap taken from the Camera application of device and save it in local storage so that it will not seen in Gallery or any other photo media sharing application.

I had done that part. But in file explorer I can see my snaps at address /storage/emulated/0/Android/data/<package name>/files** directory. Fine with it too.

The problem I am facing is to make that un-readable, for that I converted the bitmap into byteArray and write that array into a file at the same directory.

int bytes = byteSizeOf(myBitmap);

            ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
            myBitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

            byte[] byteArray = buffer.array();               

            FileOutputStream fileOuputStream = new FileOutputStream(path);
            fileOuputStream.write(byteArray);
            fileOuputStream.close();

When I am reading this file and converting it to byteArray, I am getting same byte array but when I am decoding this byteArray to Bitmap, I am getting an bitmap as null and following is showing in Logcat SkImageDecoder::Factory returned null

I am using following code to retrieve the Image bitmap

 File f = new File(path);
    try {
        byte[] byteArr = FileUtils.readFileToByteArray(f);

        Bitmap bmp;
        bmp = BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length);
        addScrollchild(bmp);
    } catch (Exception ex) {

    }

I am getting null in bmp instance.

Any help will be appriciated.

Anoop LL
  • 1,548
  • 2
  • 21
  • 32
Sunny Bansal
  • 574
  • 1
  • 4
  • 12

2 Answers2

1

Your logic for making your file unreadable is flawed. If your requirement is only to avoid showing your images in gallery application or other file explorers then simply use Context.MODE_PRIVATE when saving your image files to device. I will demonstrate a simple example to save images privately in a folder so that they don't appear in file explorers etc on a non-rooted device:

File dir = App.context().getDir("your_folder", Context.MODE_PRIVATE);
if(!dir.exists()){

    dir.mkdir();

}
File imageFile1 = new File(dir, imageName);
// save imageFile1 path to localdatabase as imageFile1.toString();
savePathToDatabase(imageFile1.toString());
byte[] imageBytes = ... // get the image bytes here
FileOutputStream out = null;
    try{
        out = new FileOutputStream(imageFile1);
        out.write(imageBytes);
       }
    catch (FileNotFoundException e){

            e.printStackTrace();
            return false;

        } catch (IOException e){

            e.printStackTrace();
            return false;
        }
    finally{
        out.close();
    }

To read these images:

// path which you saved to local database when saving images

String path = getImagePathFromTable();
if(path==null)return null;

    File f = new File(path);
    if(f.exists()){

    // get your images here how you want them.        

    }
Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • for non rooted devices this Context.PrivateMode is working fine , but this is not complete solution, its just the alternate solution as I am still getting null. – Sunny Bansal Feb 18 '16 at 12:30
  • @Sunny where are you getting `null` ? The solution provided above is working fine in one of my projects.. – Sharp Edge Feb 18 '16 at 12:34
  • in segment to read images, 'File f = new File(path); try { byte[] byteArr = FileUtils.readFileToByteArray(f); Bitmap bmp; bmp = BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length); } catch (Exception ex) { }' bitmap I am receiving is null, in logcat I am having the error SkImageDecoder::Factory returned null – Sunny Bansal Feb 19 '16 at 06:04
0

From the docs:

Returns The decoded bitmap, or null if the image could not be decode.

So, in your case, there must be a problem with saving and reading those bytes. Since you've not included details about how you got it done, I'll present bug-free ways to do these steps.

  • Convert Bitmap to byte array:

    Bitmap bmp = /*your target bitmap*/;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    
  • Write byte array to file:

    FileOutputStream fos = new FileOutputStream("filename");
    fos.write(byteArray);
    fos.close();
    
  • Read file and copy it to byte array: source

    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.nio.file.Path;
    
    Path path = Paths.get("path/to/file");
    byte[] data = Files.readAllBytes(path);
    
  • Convert byte array to Bitmap:

    Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0, array.length);
    

PS: Your approach is NOT a good way for hiding pictures from unwanted accesses. You should consider applying encryption/decryption for ensuring the security.

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • I cannot use encryption/decryption as I might be needed to show the image to the user at any time, so I cannot keep decryption in my app. – Sunny Bansal Feb 17 '16 at 08:39
  • java.nio.file.* package is not present in the android java files, other than that I am getting byte array correct, but getting error in Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0, array.length); it is giving me null. – Sunny Bansal Feb 17 '16 at 11:58