保存的SD卡图像如何显示在Android的图库中?



我将图像保存到SD卡中,但它不会出现在手机的多媒体资料中。我可以在文件夹中看到保存的图像,但它的文件夹不在图库中。

我的代码在这里,如何修复?

    img_icon.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            img_resim.buildDrawingCache();
            Bitmap bm = img_resim.getDrawingCache();
            OutputStream fOut = null;
            Uri outputFileUri;
            try {
                root = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "folder_name" + File.separator);
                root.mkdirs();
                File sdImageMainDirectory = new File(root, "myPicName.jpg");
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                fOut = new FileOutputStream(sdImageMainDirectory);
            } catch (Exception e) {
                Toast.makeText(context.getActivity(),
                        "Error occured. Please try again later.",
                        Toast.LENGTH_SHORT).show();
            }
            try {
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
            } catch (Exception e) {
            }
            context.getActivity()
                    .sendBroadcast(
                            new Intent(
                                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                    Uri.parse("file://"
                                            + Environment
                                                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
            return true;
        }
    });
    return rowView;
}

谢谢你的帮助。。

您的问题就在这里:

context.getActivity()
                .sendBroadcast(
                        new Intent(
                                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                Uri.parse("file://"
                                        + Environment
                                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));

您要求Android重新索引Environment.DIRECTORY_PICTURES中的所有文件。

要求扫描整个目录树是浪费。在您的情况下,它甚至更浪费,因为您没有将文件写入该目录。相反,您正在将该文件写入:

root = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");

因此,您的扫描不会拾取此文件,您正在将其写入外部存储器上的某个随机位置。

您需要决定存储文件的正确位置,然后为一个文件编制索引。

最新更新