簡體   English   中英

Environment.getExternalStorageDirectory()。getAbsolutePath()返回sdcard0,但是我的SD卡在sdcard1中

[英]Environment.getExternalStorageDirectory().getAbsolutePath() return sdcard0 but I have my SD in sdcard1

我嘗試使用Environment.getExternalStorageDirectory()。getAbsolutePath()將一些內容寫入SD卡電話內存,以獲取SD路徑,結果為/ storage / sdcard0。 但是我的SD卡是/ storage / sdcard1。 如何獲得真實的SD卡路徑?

我可以在“ /”中看到一個名為/ ext_sd的符號鏈接,鏈接到/ storage / sdcard1。 此符號鏈接是否存在於所有設備中?

我在這篇文章中找到了解決方案: 如何獲取Android 4.0+的外部SD卡路徑?

/**
 * Returns the list of external devices mounted.
 * 
 * @return HashSet<String>.
 */
public static HashSet<String> getExternalMounts() {
    final HashSet<String> out = new HashSet<String>();
    String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
    String s = "";
    try {
        final Process process = new ProcessBuilder().command("mount")
                .redirectErrorStream(true).start();
        process.waitFor();
        final InputStream is = process.getInputStream();
        final byte[] buffer = new byte[1024];
        while (is.read(buffer) != -1) {
            s = s + new String(buffer);
        }
        is.close();
    } catch (final Exception e) {
        e.printStackTrace();
    }

    // parse output
    final String[] lines = s.split("\n");
    for (String line : lines) {
        if (!line.toLowerCase(Locale.US).contains("asec")) {
            if (line.matches(reg)) {
                String[] parts = line.split(" ");
                for (String part : parts) {
                    if (part.startsWith("/"))
                        if (!part.toLowerCase(Locale.US).contains("vold"))
                            out.add(part);
                }
            }
        }
    }
    return out;
}

嘗試這個,

  File filepath = Environment.getExternalStorageDirectory();

                File dir = new File(filepath.getAbsolutePath()
                        + "/Save PIP/");
                dir.mkdirs();


                File file = new File(dir, System.currentTimeMillis() + ".jpg");


               // Utils.showAlert("Image Saved to SD Card", "PIP Effect", "Ok", FrameActivity.this);

                if (file.exists()) file.delete();
                try {

                    output = new FileOutputStream(file);
                    bitmap_final.compress(Bitmap.CompressFormat.PNG, 100, output);
                    output.flush();
                    output.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

在這里,您可以獲取SD卡的路徑。希望它能幫助您和您盡快解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM