在 Android 中将相机图像写入和读取到本地存储中的文件

Writing and reading a camera image to a file in local storage in Android

我想隐藏从设备的相机应用程序拍摄的快照并将其保存在本地存储中,这样它就不会在图库或任何其他照片媒体共享应用程序中看到。

我已经完成了那部分。但是在文件资源管理器中,我可以在地址 /storage/emulated/0/Android/data/<package name>/files** 目录中看到我的快照。也可以。

我面临的问题是使其不可读,为此我将位图转换为 byteArray 并将该数组写入同一目录中的文件。

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();

当我读取此文件并将其转换为 byteArray 时,我得到了相同的字节数组,但是当我将此 byteArray 解码为位图时,我得到的位图为 null,下面显示在 Logcat SkImageDecoder::Factory 返回空值

我正在使用以下代码检索图像位图

 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) {

    }

我在 bmp 实例中得到 null

我们将不胜感激。

您使文件不可读的逻辑有问题。如果您的要求只是避免在图库应用程序或其他文件浏览器中显示您的图像,那么在将图像文件保存到设备时只需使用 Context.MODE_PRIVATE。我将演示一个简单的示例,将图像私密地保存在文件夹中,这样它们就不会出现在非根设备上的文件浏览器等中:

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();
    }

阅读这些图片:

// 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.        

    }

来自docs

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

因此,在您的情况下,保存和读取这些字节一定存在问题。由于您没有包括有关如何完成它的详细信息,我将介绍执行这些步骤的无错误方法。

  • Bitmap转换为字节数组:

    Bitmap bmp = /*your target bitmap*/;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    
  • 将字节数组写入文件:

    FileOutputStream fos = new FileOutputStream("filename");
    fos.write(byteArray);
    fos.close();
    
  • 读取文件并将其复制到字节数组: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);
    
  • 将字节数组转换为Bitmap

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

PS: 您的方法不是隐藏图片以防意外访问的好方法。您应该考虑申请 encryption/decryption 以确保安全。