Android 所有我的图片对所有安卓安装的应用程序都是公开的

Android 所有我的图片对所有安卓安装的应用程序都是公开的,android,Android,我正在使用此代码将图像保存在SD卡中(模拟或真实),但我应用程序的所有测试人员都告诉我,他们的照片应用程序中有一个新文件夹,其中包含我的所有图像(我不知道为什么在我的Nexus 4和KK上没有发生这种情况) 在阅读了我在android dev页面上找到的大量内容后,是否可以更改可见性、安全性或其他内容,以避免照片应用程序或任何应用程序访问我的文件夹?? 因此,如果您希望文件保持私有,最后必须将其存储在设备中 因此,我已将代码更改为: static public void storeImage(B

我正在使用此代码将图像保存在SD卡中(模拟或真实),但我应用程序的所有测试人员都告诉我,他们的照片应用程序中有一个新文件夹,其中包含我的所有图像(我不知道为什么在我的Nexus 4和KK上没有发生这种情况)


在阅读了我在android dev页面上找到的大量内容后,是否可以更改可见性、安全性或其他内容,以避免照片应用程序或任何应用程序访问我的文件夹??

因此,如果您希望文件保持私有,最后必须将其存储在设备中

因此,我已将代码更改为:

static public void storeImage(Bitmap image, String imgName, Context context) {

    File file = new File(context.getFilesDir(), imgName);

    if (file.exists()) {
        if (file.delete()) {
            createFile(image, file);
        }
    } else {
        createFile(image, file);
    }

}

static public void createFile(Bitmap image, File file) {
    try {
        if (file.createNewFile()) {
            FileOutputStream out = new FileOutputStream(file);
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private static Boolean SdMemoryExist() {
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

static public String BasePath(Context context) {
    String _basePathToCheck;
    if (SdMemoryExist()) {
        _basePathToCheck = context.getExternalFilesDir(null).toString() + File.separator;
    } else {
        _basePathToCheck = context.getFilesDir().toString() + File.separator;
    }
    return _basePathToCheck;
}

static public void storeImage(Bitmap image, String imgName, Context context) {

    File file = new File(BasePath(context), imgName);

    if (file.exists()) {
        if (file.delete()) {
            createFile(image, file);
        }
    } else {
        createFile(image, file);
    }

}

static public void createFile(Bitmap image, File file) {
    try {
        if (file.createNewFile()) {
            FileOutputStream out = new FileOutputStream(file);
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
我希望这对某人有用


愉快的编码。

A@Sebastian pointed getExternalFilesDir(字符串类型)是一个更完整的解决方案,因此我将de代码改为:

static public void storeImage(Bitmap image, String imgName, Context context) {

    File file = new File(context.getFilesDir(), imgName);

    if (file.exists()) {
        if (file.delete()) {
            createFile(image, file);
        }
    } else {
        createFile(image, file);
    }

}

static public void createFile(Bitmap image, File file) {
    try {
        if (file.createNewFile()) {
            FileOutputStream out = new FileOutputStream(file);
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private static Boolean SdMemoryExist() {
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

static public String BasePath(Context context) {
    String _basePathToCheck;
    if (SdMemoryExist()) {
        _basePathToCheck = context.getExternalFilesDir(null).toString() + File.separator;
    } else {
        _basePathToCheck = context.getFilesDir().toString() + File.separator;
    }
    return _basePathToCheck;
}

static public void storeImage(Bitmap image, String imgName, Context context) {

    File file = new File(BasePath(context), imgName);

    if (file.exists()) {
        if (file.delete()) {
            createFile(image, file);
        }
    } else {
        createFile(image, file);
    }

}

static public void createFile(Bitmap image, File file) {
    try {
        if (file.createNewFile()) {
            FileOutputStream out = new FileOutputStream(file);
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

愉快的编码。

我认为
getExternalFilesDir(字符串类型)
是您需要的。谢谢@Sebastian你的帮助让我找到了更好的解决方案