0

我有一个使用ImageView和包含不同图像的应用程序ViewPager。我想保存通过ImageView中显示的当前图像SD Storage。但它总是Phone Storage成功保存。我想保存图像SD Card并且保存的图像没有显示在Gallery为什么?请帮助我解决这个问题:


public void SaveIamge() {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/Cute_Baby_Images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        int currentImagePos = viewPager.getCurrentItem();
        Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]);
        Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap();

        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show();

    }
4

2 回答 2

0

创建一个名为getDirectory()

private static File getDirectory(String variableName, String... paths) {
    String path = System.getenv(variableName);
    if (!TextUtils.isEmpty(path)) {
        if (path.contains(":")) {
            for (String _path : path.split(":")) {
                File file = new File(_path);
                if (file.exists()) {
                    return file;
                }
            }
        } else {
            File file = new File(path);
            if (file.exists()) {
                return file;
            }
        }
    }
    if (paths != null && paths.length > 0) {
        for (String _path : paths) {
            File file = new File(_path);
            if (file.exists()) {
                return file;
            }
        }
    }

    //If any there is no SECONDARY STORAGE are detected return INTERENAL STORAGE
    return Environment.getExternalStorageDirectory();
}

将您的代码更改为

public void SaveIamge() {

    String root = getDirectory("SECONDARY_STORAGE").getAbsolutePath();
    File myDir = new File(root + "/Cute_Baby_Images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    int currentImagePos = viewPager.getCurrentItem();
    Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]);
    Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap();

    if (file.exists ()) file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show();
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + root)));
}

我添加了一个broadcast通知MediaScanner重新扫描文件系统以查找新文件。这将解决图片未在图库中显示的问题。

于 2017-10-11T04:03:32.440 回答
0

Environment.getExternalDirectory()返回手机存储。要获取 SD 卡的位置,请检查查找外部 SD 卡位置

要使图像出现在图库中,您应该让媒体扫描仪使用MediaScannerConnection.scanFile() 检查图像扫描文件,保存到 sdcard,不会出现在 Android 的图库应用中

于 2017-10-11T02:36:10.250 回答