【问题标题】:Saving image from ImageView to storage将图像从 ImageView 保存到存储
【发布时间】:2020-08-09 06:19:29
【问题描述】:

我遵循了Saving an image from ImageView into internal storage 的答案,但我仍然无法保存任何东西。我的代码在这里:

 public void buttonPickImage(View view) {

        FileOutputStream fos;

        bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

        Random rng = new Random();
        int n = rng.nextInt(1000);




        try {

            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath() + "/BAC");
            bool = dir.mkdir();

            File file = new File(dir, "BAC_"+n+".jpg");


            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
            fos.flush();
            fos.close();
            Toast.makeText(getApplicationContext(),"Image sauvegardée"+bool,Toast.LENGTH_SHORT).show();

        }catch (java.io.IOException e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"IOException: " + e.getMessage(),Toast.LENGTH_SHORT).show();
        }

    }

通过这种方法,我得到带有消息的 IOExeception :java.io.FileNotFoundException: /storage/emulated/0/BAC/BAC_396.jpg: open failed: ENOENT (No such file or directory)

我也尝试将其保存到内部存储,但它不适合我: https://www.tutorialspoint.com/how-to-write-an-image-file-in-internal-storage-in-android 使用这种方法,程序运行但布尔 mkdir 给我 false。

谢谢你帮助我

【问题讨论】:

  • dir.mkdir(); 仅当目录不存在时才调用 mkdir。如果您检查返回值。如果它返回 false,则显示一个 toast 以通知用户。并返回。不要继续,因为如果目录不存在,那将毫无意义。请尝试并在此处调整您的代码。
  • 您没有知道显示的是哪个吐司。那么我们怎么知道呢?
  • "not working"。那应该是"IOException: " + e.getMessage(),以便更好地通知用户。
  • 您也没有从 logcat 中发布相关行。您可能有 IOException 但没有通知我们。
  • to internal storage but its not working for me 那么发生了什么?有什么错误?

标签: android imageview storage


【解决方案1】:

终于可以使用 Media Store 代替 getExternalStorageDirectory as

此方法在 API 级别 29 中已弃用。 为了提高用户隐私,不推荐直接访问共享/外部存储设备。当应用程序以 Build.VERSION_CODES.Q 为目标时,从此方法返回的路径不再可供应用程序直接访问。通过迁移到 Context#getExternalFilesDir(String)、MediaStore 或 Intent#ACTION_OPEN_DOCUMENT 等替代方案,应用可以继续访问存储在共享/外部存储上的内容。

MediaStore 也很有用,因为它允许您在 android 图库应用中获取图像。

所以我的解决方案是:

ImageView imageView = findViewById(R.id.image);
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "any_picture_name");
        values.put(MediaStore.Images.Media.BUCKET_ID, "test");
        values.put(MediaStore.Images.Media.DESCRIPTION, "test Image taken");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        OutputStream outstream;
        try {
            outstream = getContentResolver().openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
            outstream.close();
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
        }

仍然感谢@blackapps 向我解释了一些关于 IOexception、mkdir 和 toasts 的基本知识。无论如何都会有用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2014-09-26
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多