在我的应用程序中,可绘制文件夹中有几张图像。这使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();
    }

}


编辑

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

有关更多信息,Internal Storage

关于java - Android。在哪里保存下载的图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25216220/

10-11 22:12
查看更多