Android 打开外部存储目录(sdcard)用于存储文件 [英] Android Open External Storage directory(sdcard) for storing file

查看:61
本文介绍了Android 打开外部存储目录(sdcard)用于存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式打开外部存储目录路径以保存文件.我尝试过但没有获得 sdcard 路径.我该怎么做?有什么解决办法吗?

I want to open external storage directory path for saving file programatically.I tried but not getting sdcard path. How can i do this?is there any solution for this??

private File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "");

private File path = new File(Environment.getExternalStorageDirectory() + "");

我尝试从上面两种方法中获取路径,但都指向内部存储器.

I tried getting path from above both methods but both are pointing internal memory.

当我们打开存储内存如果sdcard是peresent它会显示如下-

When we open storage memory if sdcard is peresent it will shows like below-

设备存储和sd存储卡.

device storage & sd memory card.

我想通过编码获得 sd 内存路径.我已在清单中授予权限-

I want to get sd memory path through coding. I have given permissions in manifest-

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

推荐答案

我遇到了完全相同的问题!

I had been having the exact same problem!

要获取您可以使用的内部 SD 卡

To get the internal SD card you can use

String extStore = System.getenv("EXTERNAL_STORAGE");
File f_exts = new File(extStore);

要获取可以使用的外部 SD 卡

To get the external SD card you can use

String secStore = System.getenv("SECONDARY_STORAGE");
File f_secs = new File(secStore);

运行代码

 extStore = "/storage/emulated/legacy"
 secStore = "/storage/extSdCarcd"

完美运行!

这篇关于Android 打开外部存储目录(sdcard)用于存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆