繁体   English   中英

Android。 在哪里保存下载的图像?

[英]Android . Where to save downloaded images?

在我的应用程序中,可绘制文件夹中有几张图像。 这使apk大。 现在,我已将它们上传到我的Google驱动器中,当用户连接到互联网时,它将从驱动器下载该图像。 在哪里保存下载的图像? 在外部存储,数据库还是其他地方? 我希望用户无法删除该图像。

我希望用户无法删除该图像

你真的不能那样做。 除非用户选择了图像,否则这些图像不会出现在此处,那么为什么不希望用户删除它们? 在这种情况下,您将再次下载它或要求用户选择。 正常的“缓存”场景。 更不用说您实际上无法保护这些图像,因为用户始终可以清除应用程序数据(包括数据库和内部存储)。

您可以将它们存储在内部电话存储器中。

保存图像

private String saveToInternalSorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("youDirName", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"img.jpg");

        FileOutputStream fos = null;
        try {           

            fos = new FileOutputStream(mypath);

       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return directory.getAbsolutePath();
    }

访问存储的文件

private void loadImageFromStorage(String path)
{

    try {
        File f = new File(path, "img.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        // here is the retrieved image in b
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

}

编辑

您可以将文件直接保存在设备的内部存储中。 默认情况下,保存到内部存储器的文件是您的应用程序专用的,其他应用程序无法访问它们(用户也不能访问它们)。 当用户卸载您的应用程序时,这些文件将被删除。

有关更多信息, 内部存储

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM