【问题标题】:How to save captured photo in application directory in Android如何将捕获的照片保存在Android的应用程序目录中
【发布时间】:2014-05-12 10:06:46
【问题描述】:

您好,我想将拍摄的照片保存在 SQLite 等所在的应用程序目录中。我不想在 SD 卡中创建任何文件夹和任何隐藏文件。如何将捕获的照片保存在应用程序目录中。

我在下面尝试这个,但它保存在 SD 卡中是隐藏文件,但我不想要这种方法。

Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String capturedPhotoName = "." + System.currentTimeMillis() + ".png";
File photo = new File(Environment.getExternalStorageDirectory(),
        capturedPhotoName);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, CAMERA_INTENT_REQUEST);

提前致谢!

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    尝试此方法将捕获的图像保存在应用程序存储中

     public Uri setImageUri() {
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            File file = new File(directory,System.currentTimeMillis() + ".png");
            Uri imgUri = Uri.fromFile(file);
            this.imgPath = file.getAbsolutePath();
            return imgUri;
        }
    

    图像将存储在此位置

    /data/data/com.your.packagename/app_data/imageDir
    

    【讨论】:

    • 应用程序cacheDir的最大大小是多少?,保存多个大文件不会造成任何问题?
    • 文档中没有声明修复大小,它取决于设备内部存储
    • 哦,如果设备内部存储为 16 GB,那么它的缓存大小是多少?
    • 文档说如果你想缓存一些数据,而不是永久存储它,你应该使用 getCacheDir() 打开一个文件,它代表你的应用程序应该保存临时缓存文件的内部目录。当设备内部存储空间不足时,Android 可能会删除这些缓存文件以恢复空间。但是,您不应依赖系统为您清理这些文件。您应该始终自己维护缓存文件并保持在合理的空间消耗限制范围内,例如 1MB。当用户卸载您的应用程序时,这些文件将被删除。
    • 所以它说要保存大约 1 MB 大小的东西,但这里捕获的照片大小会超过 2 MB,那么该怎么做呢?
    【解决方案2】:

    从 onActivityResult() 中获取 URI 并将其转换为字节并将字节存储在数据库中。

    byte[] UserPic = null;
    UserPic = Base64.decode(result.getData(), 0);
    

    将用户图片保存在数据库中

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      相关资源
      最近更新 更多