androidsd路径获取
‘壹’ 如何正确获得Android内外SD卡路径
/**
* 获取手机自身内存路径
*
*/
public static String getPhoneCardPath(){
return Environment.getDataDirectory().getPath();
}
/**
* 获取sd卡路径
* 双sd卡时,根据”设置“里面的数据存储位置选择,获得的是内置sd卡或外置sd卡
* @return
*/
public static String getNormalSDCardPath(){
return Environment.getExternalStorageDirectory().getPath();
}
/**
* 获取sd卡路径
* 双sd卡时,获得的是外置sd卡
* @return
*/
public static String getSDCardPath() {
String cmd = "cat /proc/mounts";
Runtime run = Runtime.getRuntime();// 返回与当前 java 应用程序相关的运行时对象
BufferedInputStream in=null;
BufferedReader inBr=null;
try {
Process p = run.exec(cmd);// 启动另一个进程来执行命令
in = new BufferedInputStream(p.getInputStream());
inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
while ((lineStr = inBr.readLine()) != null) {
// 获得命令执行后在控制台的输出信息
Log.i("CommonUtil:getSDCardPath", lineStr);
if (lineStr.contains("sdcard")
&& lineStr.contains(".android_secure")) {
String[] strArray = lineStr.split(" ");
if (strArray != null && strArray.length >= 5) {
String result = strArray[1].replace("/.android_secure",
"");
return result;
}
}
// 检查命令是否执行失败。
if (p.waitFor() != 0 && p.exitValue() == 1) {
// p.exitValue()==0表示正常结束,1:非正常结束
Log.e("CommonUtil:getSDCardPath", "命令执行失败!");
}
}
} catch (Exception e) {
Log.e("CommonUtil:getSDCardPath", e.toString());
//return Environment.getExternalStorageDirectory().getPath();
}finally{
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(inBr!=null){
inBr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Environment.getExternalStorageDirectory().getPath();
}
//查看所有的sd路径
public String getSDCardPathEx(){
String mount = new String();
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.contains("secure")) continue;
if (line.contains("asec")) continue;
if (line.contains("fat")) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
mount = mount.concat("*" + columns[1] + "\n");
}
} else if (line.contains("fuse")) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
mount = mount.concat(columns[1] + "\n");
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mount;
}
//获取当前路径,可用空间
public static long getAvailableSize(String path){
try{
File base = new File(path);
StatFs stat = new StatFs(base.getPath());
long nAvailableCount = stat.getBlockSize() * ((long) stat.getAvailableBlocks());
return nAvailableCount;
}catch(Exception e){
e.printStackTrace();
}
return 0;
}
‘贰’ Android开发中,获取U盘或SD卡路径。
@SuppressLint("PrivateApi")
private String getStoragePath(Context context, boolean isUsb){
String path="";
StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Class<?> volumeInfoClazz;
Class<?> diskInfoClaszz;
try {
volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo");
diskInfoClaszz = Class.forName("android.os.storage.DiskInfo");
Method StorageManager_getVolumes=Class.forName("android.os.storage.StorageManager").getMethod("getVolumes");
Method VolumeInfo_GetDisk = volumeInfoClazz.getMethod("getDisk");
Method VolumeInfo_GetPath = volumeInfoClazz.getMethod("getPath");
Method DiskInfo_IsUsb = diskInfoClaszz.getMethod("isUsb");
Method DiskInfo_IsSd = diskInfoClaszz.getMethod("isSd");
List<Object> List_VolumeInfo = (List<Object>) StorageManager_getVolumes.invoke(mStorageManager);
assert List_VolumeInfo != null;
for(int i=0; i<List_VolumeInfo.size(); i++){
Object volumeInfo = List_VolumeInfo.get(i);
Object diskInfo = VolumeInfo_GetDisk.invoke(volumeInfo);
if(diskInfo==null)continue;
boolean sd= (boolean) DiskInfo_IsSd.invoke(diskInfo);
boolean usb= (boolean) DiskInfo_IsUsb.invoke(diskInfo);
File file= (File) VolumeInfo_GetPath.invoke(volumeInfo);
if(isUsb == usb){//usb
assert file != null;
path=file.getAbsolutePath();
}else if(!isUsb == sd){//sd
assert file != null;
path=file.getAbsolutePath();
}
}
} catch (Exception e) {
YYLog.print(TAG, "[——————— ——————— Exception:"+e.getMessage()+"]");
e.printStackTrace();
}
return path;
}
‘叁’ android开发内置存储设备外置SD卡的路径怎么获取
方法:1点击手机的文件管理2点击上方的本地选项3显示内部储存的是自带储存路径,SD卡位sdcard的储存路径
‘肆’ android怎么获sd卡内的所有文件目录
android程序获取SD卡目录的方法如下:
手机通过数据线连接电脑端,在计算机里会显示两个磁盘,一个是系统内存,另外一个就是SD卡内存,打开SD卡就可以找到文件目录。
也可以通过手机查找,打开手机应用程序,点击文件管理打开,然后打开所有文件。
接着打开”extSdCard“文件夹就是SD卡里的目录了。
‘伍’ android开发想得到sd卡路径
通过正规api得不到外卡路径.
谷歌在源码中给出了得到外卡路径的方法,但标记为隐藏接口,因此api无法访问.
可以通过反射接口得到:
importjava.lang.reflect.Method;
importandroid.os.storage.StorageManager;
(){
try{
StorageManagersm=(StorageManager)getSystemService(STORAGE_SERVICE);
MethodgetVolumePathsMethod=StorageManager.class.getMethod("getVolumePaths",null);
String[]paths=(String[])getVolumePathsMethod.invoke(sm,null);
//firstelementinpaths[]isprimarystoragepath
returnpaths[0];
}catch(Exceptione){
Log.e(TAG,"getPrimaryStoragePath()failed",e);
}
returnnull;
}
(){
try{
StorageManagersm=(StorageManager)getSystemService(STORAGE_SERVICE);
MethodgetVolumePathsMethod=StorageManager.class.getMethod("getVolumePaths",null);
String[]paths=(String[])getVolumePathsMethod.invoke(sm,null);
//secondelementinpaths[]issecondarystoragepath
returnpaths[1];
}catch(Exceptione){
Log.e(TAG,"getSecondaryStoragePath()failed",e);
}
returnnull;
}
publicStringgetStorageState(Stringpath){
try{
StorageManagersm=(StorageManager)getSystemService(STORAGE_SERVICE);
MethodgetVolumeStateMethod=StorageManager.class.getMethod("getVolumeState",newClass[]{String.class});
Stringstate=(String)getVolumeStateMethod.invoke(sm,path);
returnstate;
}catch(Exceptione){
Log.e(TAG,"getStorageState()failed",e);
}
returnnull;
}
如果楼主有源码,可以去找StorageManager这个类,拉到文件最下方,就可以看到那三个隐藏接口.
‘陆’ android 几种不同路径的获取方法
前两个应用内部存储通过 Context 来获取, 第三个作为外部存储是通过 Environment 类来获取. 注释为返回值.
/data/data/包名/
context.getFilesDir(); // /data/data/包名/filescontext.getCacheDir(); // /data/data/包名/cache/sdcard/Android/data/包名/
context.getExternalFilesDir(); // /sdcard/Android/data/包名/filescontext.getExternalCacheDir(); // /sdcard/Android/data/包名/cache/sdcard/xxx
// /storage/emulated/0Environment.getExternalStorageDirectory();// /storage/emulated/0/DCIM, 另外还有MOVIE/MUSIC等很多种标准路径Environment.(Environment.DIRECTORY_DCIM);
注意, 根据源码文档中说明, 获取外部存储时, 有可能会因为各种问题导致获取失败, 建议先使用 getExternalStorageState 来判断外部存储状态, 如果已挂载的话再存储.
‘柒’ 获取android手机的自带存储路径和sdcard存储路径
android手机获取自带存储来路径和sd卡存储路径的方式是:调用Environment.getExternalStorageDirectory(),返回的存储源目录并不是系统内置的SD卡目录。
1.一部分手机将eMMC存储挂载到
/mnt/external_sd
、/mnt/sdcard2
等节点知,而将外置的SD卡挂载到
Environment.getExternalStorageDirectory()这个结点。
此时,调用Environment.getExternalStorageDirectory(),则返回外置的SD的路径。
2.而另一部分手机直接道将eMMC存储挂载在Environment.getExternalStorageDirectory()这个节点,而将真正的外置SD卡挂载到/mnt/external_sd、/mnt/sdcard2
等节点。
此时,调用Environment.getExternalStorageDirectory(),则返回内置的SD的路径。
‘捌’ xamarin怎样获取android外卡路径
xamarin怎样获取android外卡路径
一部分手机将eMMC存储挂载到 /mnt/external_sd 、/mnt/sdcard2 等节点,而将外置的SD卡挂载到 Environment.getExternalStorageDirectory()这个结点。
此时,调用Environment.getExternalStorageDirectory(),则返回外置的SD的路径。
android 怎样动态的获取sd卡路径
链接电脑USB调试情况下,在电脑上有新的磁盘显示,一个SD的,一个手机的,打开SD的就可以找各文件夹路径
Android怎样获取外部存储器路径
读写sdcard上的文件
其中读写步骤按如下进行:
1、调用Environment的getExternalStorageState()方法判断手机上是否插了sd卡,且应用程序具有读写SD卡的权限,如下代码将返回true
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
2、调用Environment.getExternalStorageDirectory()方法来获取外部存储器,也就是SD卡的目录,或者使用"/mnt/sdcard/"目录
3、使用IO流操作SD卡上的文件
注意点:手机应该已插入SD卡,对于模拟器而言,可通过mksdcard命令来创建虚拟存储卡
必须在AndroidManifest.xml上配置读写SD卡的权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我怎样才能获得外部SD卡路径为Android 4.0 +
1. 我想你需要这个外部SD卡:
new File("/mnt/external_sd/")
或
new File("/mnt/extSdCard/")
你的情况......
在更换Environment.getExternalStorageDirectory()您的作品,应在MNT目录优先和工作在那里检查什么..
你键入选择哪个SD卡
File storageDir = new File("/mnt/");
if(storageDir.isDirectory()){
String[] dirList = storageDir.list();
TODO some type of selecton method?
}
怎样获取class路径
- importjava.io.File;
-
publicclassFileTest{
-
publicstaticvoidmain(String[]args)throwsException{
-
System.out.println(Thread.currentThread().getContextClassLoader()
-
.getResource(""));
-
System.out.println(FileTest.class.getClassLoader().getResource(""));
-
System.out.println(ClassLoader.getSystemResource(""));
-
System.out.println(FileTest.class.getResource(""));
-
System.out.println(FileTest.class.getResource("/"));
-
Class文件所在路径
-
System.out.println(newFile("/").getAbsolutePath());
-
System.out.println(System.getProperty("user.dir"));
-
System.out.println(System.getProperty("file.encoding"));
-
}
-
}
android 怎么获取相册路径
android手机4.2版本之前是一个方法,大于4.2版本又是一个方法。
注意:现在手机市场android版本2015面上半年平均4.4,现在是平均5.0了。
-
before
你网络一下,都能查到,很简单.
-
after
private void startPickPhotoActivity() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("image/*"); Or 'image/ jpeg '
startActivityForResult(intent, RESULT_PICK_PHOTO_NORMAL);
}
获得图片返回的路径
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == RESULT_PICK_PHOTO_NORMAL) {
if (resultCode == RESULT_OK && data != null) {
选中图片路径
mFileName = MainActivity.getPath(getApplicationContext(),
data.getData());
if ("".equals(mFileName)) {
return;
}
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("pathName", mFileName);
startActivity(intent);
}
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
ExternalStorageProvider
if (UriUtils.isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
}
DownloadsProvider
else if (UriUtils.isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content:downloads/public_downloads"),
Long.valueOf(id));
return UriUtils.getDataColumn(context, contentUri, null, null);
}
MediaProvider
else if (UriUtils.isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = MediaColumns._ID + "=?";
final String[] selectionArgs = new String[] { split[1] };
return UriUtils.getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
Return the remote address
if (UriUtils.isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return UriUtils.getDataColumn(context, uri, null, null);
}
File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
如何获取android sdcard路径
Environment.getExternalStorageDirectory()
android怎么获取U盘路径
用Re管理器进入/mnt/目录一个一个找
android怎么获取分区路径
我们可以在adb中使用df 来查看分区情况。
1、跨分区不能用 MV命令来拷贝。但是可以用CP命令。如PWD,当前目录为:/data/local/tmp 。
此目录下有个busybox和1.txt文件。则利用CP命令拷贝如下:./busybox cp1.txt /system.
2、Android的用户组有 System, root, shell
3、怎么样才能操作分区。
分区操作是需要权限的。一般来说System分区的权限限制得比较严,Data分区限制比较严,用户可以操作的目录有local,app目录。比如/data/local/tmp.
data分区常用目录:app , system , data ,local,misc 其中system,local可以进入并使用ls等命令。data,app可以进入,但不能用ls命令。
data/data目录:存放的是所有APK程序数据的目录,每个APK对就一个自己的Data目录,就是在data/data/目录下,会产生一个跟Package一样的目录。如有一个APK,它的包名叫.test.hello则,在data/data/目录下会有一个.test.hello的目录,这个APK只能操作此目录,不能操作其它APK的目录.这个在LINUX下叫做用户进程只能操作自己的进程目录.
data/app目录:用户安装的APK放在这里。我们如果把APK放入这个文件夹下面的话,就算安装好了。这就叫静默安装。不用管APK文件里面的lib目录下的库文件,系统会自动帮我们放入调用库的。
data/system目录下面有packages.xml ,packages.list,appwidgets.xml, 等等一些记录手机安装的软件,Widget等信息。
data/misc目录:保存WIFI帐号,VPN设置信息等。如保存了一个WIFI连接帐号,则此目录下的WIFI目录下面可以查看到。
system分区常用目录: app , lib, xbin, bin , media,framework.
system/app目录:存放系统自带的APK。没有测试过是否将APK放入到System/app目录下,也是静默安装APK。?
system/lib目录:存放APK程序用到的库文件。
system/bin目录和system/xbin目录:存放的是shell命令。
system/framework目录:启用Android系统所用到框架,如一些jar文件。
4 Android下面的目录都是有权限的,要操作目录都需要有此权限才能操作,如果没有,则使用chomd777来修改.如果是分区根目录,如/data分区, /system分区.都没有权限,则需要重新挂载. 使用 mount -oremount XX
‘玖’ android4.0后怎么获取sdcard的路径(包括外置和内置的)
getExternalStorageDirectory()方法在4.0以后只能获取内置SD卡路径
外置SD卡
/**
* 获取外置SD卡路径
* @return 应该就一条记录或空
*/
public List<string> getExtSDCardPath()
{
List<string> lResult = new ArrayList<string>();
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
if (line.contains("extSdCard"))
{
String [] arr = line.split(" ");
String path = arr[1];
File file = new File(path);
if (file.isDirectory())
{
lResult.add(path);
}
}
}
isr.close();
} catch (Exception e) {
}
return lResult;
}
List<string> extPaths = getExtSDCardPath();
for (String path : extPaths) {
log.append("外置SD卡路径:" + path + "\r\n");
}
PS别忘记添加权限,内外置SD卡的权限在4.0以后是不一样的
‘拾’ 安卓手机sd卡的路径在哪
sd卡路径的确是在/mnt sdcard,不过请注意下载游戏安装玩是无法安装在sd卡里,也就是说你安装了还是在手机内存里,游戏运行时是占用手机内存的,建议你试试用电脑把数据包下载在手机sd卡里,如果还是提醒内存不足就卸载掉点东西吧。
(10)androidsd路径获取扩展阅读:
SD存储卡是一种基于半导体快闪记忆器的新一代记忆设备,由于它体积小、数据传输速度快、可热插拔等优良的特性,被广泛地于便携式装置上使用,例如数码相机、个人数码助理(外语缩写PDA)和多媒体播放器等。
SD卡是由松下电器、东芝和SanDisk联合推出,1999年8月发布。SD卡的数据传送和物理规范由MMC发展而来,大小和MMC卡差不多,尺寸为32mm x 24mm x 2.1mm。长宽和MMC卡一样,只是比MMC卡厚了0.7mm,以容纳更大容量的存贮单元。
S与 MMC 卡保持着向上的兼容,MMC卡可以被新的SD设备存取,兼容性则取决于应用软件,但SD卡却不可以被MMC设备存取。
参考资料:SD卡-网络