android - 在图库中显示保存的图像

标签 android

我正在创建一个目录来存储位图图像。这是我的代码:

 String root = Environment.getExternalStorageDirectory().toString();
                File myDir = new File(root + "/ABC");
                myDir.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-" + n + ".jpg";
                File file = new File(myDir, fname);
                Log.i(TAG, "" + file);
                if (file.exists())
                    file.delete();
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

但是这个文件夹没有显示在图库中。我使用的是 Android“Marshmallow”版本。有人可以帮助我吗?

最佳答案

使用这个广播

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    Intent mediaScanIntent = new Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri contentUri = Uri.fromFile(out); //out is your file you saved/deleted/moved/copied
                    mediaScanIntent.setData(contentUri);
                    this.sendBroadcast(mediaScanIntent);
                } else {
                    sendBroadcast(new Intent(
                            Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                }

请引用此链接:link to help

关于android - 在图库中显示保存的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39444490/

相关文章:

android - AndEngine 出现 Dialog 时更新停止

android - https在android中获取400 Bad Request的调用结果

android - 在不调用弹出 fragment 的 onCreateView 的情况下清除 fragment 堆栈

android - 从 Android 预启动报告中禁用 Google Analytics

android - 切换到 Camera Intent 时应用程序类数据被清零

java - 在我的 Android 设备上,按下按钮时录制的音频缺少一些持续时间

java.lang.IndexOutOfBoundsException : setSpan (118 . .. 119) 结束超过长度 118

android - 回收站 View 23.2.0 快速滚动

android - 在 SSLContext 中使用硬件支持的 key

java - 将自定义 ListView 添加到 fragment