【问题标题】:Image is not showing in gallery after save保存后图片未显示在图库中
【发布时间】:2019-01-16 11:34:30
【问题描述】:

您好,我正在尝试将图像保存在我的画廊中,但问题是,无法在我的画廊中看到,以下是我的代码,任何人都可以提供帮助吗?

public void OnClickSave(View view)
    {
        Bitmap bitmap =getBitmapFromView(idForSaveView);

        try {


            ContextWrapper wrapper = new ContextWrapper(context);

            File file = wrapper.getDir("MilMilaImages",MODE_PRIVATE);

            // Create a file to save the image
            file = new File(file, "MilMila"+".jpg");

            try{

                OutputStream stream = null;

                stream = new FileOutputStream(file);


                bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);


                stream.flush();


                stream.close();

            }catch (IOException e) // Catch the exception
            {
                e.printStackTrace();
            }

            // Parse the gallery image url to uri
            final Uri savedImageURI = Uri.parse(file.getAbsolutePath());

            // Display the saved image to ImageView
            System.out.println("HLL"+savedImageURI);

            iv.setImageURI(savedImageURI);



            MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath()},
                    null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {

                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Intent mediaScanIntent = new Intent(
                        Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri contentUri = Uri.fromFile(file);
                mediaScanIntent.setData(contentUri);
                context.sendBroadcast(mediaScanIntent);
            } else {
                context.sendBroadcast(new Intent(
                        Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://"
                                + Environment.getRootDirectory())));
            }

            // Display saved image uri to TextView

//            Toast.makeText(context,"Saved Successfully",Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

  • 您有写入外部存储的权限吗?
  • 是的,我已经添加了这个权限

标签: android file android-gallery android-mediascanner


【解决方案1】:

我已经使用了这段代码,它对我有用:

// show the image in the device gallery
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
        val contentUri = Uri.fromFile(compressFile) //out is your output file
        mediaScanIntent.data = contentUri
        this.sendBroadcast(mediaScanIntent)
    } else {
        sendBroadcast(Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())))
    }// // show the image in the device gallery

【讨论】:

  • 这是用于外部存储的
  • @AdityaVyas-Lakhan 如果我按 ctrl + 右键单击​​查看描述而不是它说的 * 返回主共享/外部存储目录。如果此目录 * 已被用户安装在他们的计算机上,已从设备中删除,或者发生了其他问题,则 * 当前可能无法访问。您可以使用 * {@link #getExternalStorageState()} 确定其当前状态。 *

    *注意:不要被这里的“外部”这个词弄糊涂了.....更多你可以通过 ctrl + 右键阅读它

  • 是的,我也检查过,也试过你的代码。但仍然在我的画廊中,它没有显示图像
  • @AdityaVyas-Lakhan 您在 imageview 中获得了正确的图像,但没有存储在画廊中,对吗?您正在测试哪款手机?
  • 是的。我正在使用 Moto e4
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
相关资源
最近更新 更多