在Lollipop上以编程方式隐藏SD卡上的图像

时间:2015-12-10 03:08:39

标签: java android image hide

我试图在SD卡上隐藏一些图像,但它们并未隐藏 我尝试了没有成功的代码。

请帮帮我。

===================代码==============

Log.e("old path", "" + mTempArry.get(i));//get All file pathe

File fileinDirectory = new File(mTempArry.get(i));

File newfilee = new File(hideImage + fileinDirectory.getName());
Log.e("new path", "" + hideImage + fileinDirectory.getName());

boolean d = fileinDirectory.renameTo(newfilee);

if (d) {
    try {

        getContentResolver()
                .delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        MediaStore.Images.Media.DATA + "='"
                                + fileinDirectory.getPath()
                                + "'", null);
        db.insertImage(fileinDirectory.getName(),
                fileinDirectory.getPath());
    } catch (Exception e) {

        e.printStackTrace();

    }

    filesmoved = filesmoved + 1;

    publishProgress(filesmoved + "/" + totalFilesToMove);
} else {

    renameFileExtension(mTempArry.get(i), "jpg");

    try {
        copyFile(fileinDirectory, newfilee);
        getContentResolver()
                .delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        MediaStore.Images.Media.DATA + "='"
                                + fileinDirectory.getPath()
                                + "'", null);

        db.insertImage(fileinDirectory.getName(),
                fileinDirectory.getPath());

        // renameFileExtension(mTempArry.get(i), "txt");

        fileinDirectory.delete();

        Log.e("rename", "" + d);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

1 个答案:

答案 0 :(得分:2)

File fileFrom = new File(Environment.getExternalStorageDirectory(),"image.jpg");
File fileTo = new File(Environment.getExternalStorageDirectory(), "." + "image.jpg");
fileFrom.renameTo(fileTo);

它基本上重命名文件并以点(。)开头。由于android具有linux内核,因此它隐藏了以点(。)开头的每个文件。

确保您在AndroidManifest.xml

中拥有这些权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

另请注意,Environment.getExternalStorageDirectory()表示SDCard上图像的文件夹。