【问题标题】:What is the best way to store images locally in android?在android中本地存储图像的最佳方法是什么?
【发布时间】:2019-02-13 22:51:21
【问题描述】:

我想创建一个离线应用程序,用户可以在其中添加带有图像细节的图像。我想知道离线存储图像的最佳方式是什么?

我应该使用 SQLite blob 格式、SQLite base64 编码的字符串还是应该将其存储在内部存储中还是应该将其存储在共享首选项中。请帮助我,如果您知道任何其他方式,请提及。

【问题讨论】:

  • "Should I use SQLite blob format, SQLite base64 encoded String or should I store it in internal storage" 在大多数情况下,最后一个选项是最好的

标签: java android android-layout android-fragments


【解决方案1】:

内部存储是最好的。 如果您想保持私密,请更改图像格式然后保存 否则按原样保存

【讨论】:

    【解决方案2】:

    要在本地保存图像,请使用以下代码:

    private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.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);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        } 
        return directory.getAbsolutePath();
    }
    
    猜你喜欢
    • 2013-02-04
    • 2013-05-23
    • 2011-08-09
    • 1970-01-01
    • 2021-06-28
    • 2020-11-23
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    相关资源
    最近更新 更多