强制访问外部可移动microSD卡 [英] Force access to external removable microSD card
问题描述
我使用的是三星A3,Android 5.0.2。我正在使用
但您可以使用某种方法获取可移动microSD卡的真实路径:
public static String getRemovableSDCardPath(Context context){
File [] storages = ContextCompat.getExternalFilesDirs(context,null);
if(storages.length> 1&& storages [0]!= null&& storages [1]!= null)
return storages [1] .toString();
else
返回;
}
然后就这样做:
文件myDir = new File(getRemovableSDCardPath(getApplicationContext()),test);
if(myDir.mkdirs()){
Log.i(TAG,目录成功创建!);
} else {
Log.i(TAG,创建目录时出错!);
}
例如使用方法:
String pathSDCard = getRemovableSDCardPath(getApplicationContext());
因此我的可移动SD卡的路径(如果我不可移动的话) SD卡我的路径是,所以你可以实现验证以避免创建文件夹):
/ storage /extSdCard/Android/data/com.jorgesys.myapplication/files
现在在里面创建一个新文件夹:
文件myDir = new File(getRemovableSDCardPath(getApplicationContext()),test);
if(myDir.mkdirs()){
Log.i(TAG,目录成功创建!);
} else {
Log.i(TAG,创建目录时出错!);
}
现在我的目录是 / test
已创建:
< img src =https://i.stack.imgur.com/bReWz.pngalt =在此处输入图像说明>
I'm using a Samsung A3, Android 5.0.2. I'm using this setup to compile apps, i.e. Android 4.1 Jelly Bean (API 16) target.
I precisely know the path of the external removable microSD card, it is /mnt/extSdCard/
(see also Note #7 below).
Problem: I notice that
File myDir = new File("/mnt/extSdCard/test");
myDir.mkdirs();
doesn't work: no directory is created.
Also:
File file = new File("/mnt/extSdCard/books/test.txt"); // the folder "books" already exists on the external microSD card, has been created from computer with USB connection
FileOutputStream fos = new FileOutputStream(file);
produces this error:
java.io.FileNotFoundException: /mnt/extSdCard/books/test.txt: open failed: EACCES (Permission denied) at libcore.io.IoBridge.open(...
How to force read+write access to external removable microSD card?
Notes:
Environment.getExternalStorageDirectory().toString()
gives/storage/emulated/0
which is my phone internal storage, i.e. not what I want.getExternalFilesDir(null)
gives/storage/emulated/0/Android/data/com.blahblah.appname/files/
i.e. not what I want. Note that I can't usegetExternalFilesDirs
with a finals
because this is not available in API16. Also runtime permissions are not available in API16 neither.I already have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
, and alsoREAD_EXTERNAL_STORAGE
.I read lots of topics like this one or this one, in fact probably twenty similar questions, but finally it seems very complex and everything and its contrary is said. That's my I'm looking for a solution specific to this situation.
I don't want
ACTION_OPEN_DOCUMENT
andACTION_CREATE_DOCUMENT
, in fact I don't want any GUI solution.Some apps I have (Sync Resilio) are allowed to modify
/mnt/extSdCard/music/
successfully, to create new files there, etc.By the way,
ls -la /mnt/extSdCard/
givesdrwxrwx--x root sdcard_r 2017-10-15 01:21 Android drwxrwx--- root sdcard_r 2017-10-14 00:59 LOST.DIR drwxrwx--- root sdcard_r 2017-12-05 16:44 books drwxrwx--- root sdcard_r 2017-11-21 22:55 music
bear in mind that some android devices will have a different path for the SD Card and some doesn´t have removable SD Card.
You don´t have to set the path directly!
File myDir = new File("/mnt/extSdCard/test");
myDir.mkdirs();
You can check first if your device has mounted a removable SD Card:
public static boolean isSDCardAvailable(Context context) {
File[] storages = ContextCompat.getExternalFilesDirs(context, null);
if (storages.length > 1 && storages[0] != null && storages[1] != null)
return true;
else
return false;
}
Why get External Directories > 1, well because most of all the android devices has external storage as a primary directory and removable SD Card as second directory:
But you can use a method to get the real path of your removable microSD card:
public static String getRemovableSDCardPath(Context context) {
File[] storages = ContextCompat.getExternalFilesDirs(context, null);
if (storages.length > 1 && storages[0] != null && storages[1] != null)
return storages[1].toString();
else
return "";
}
Then just do this:
File myDir = new File(getRemovableSDCardPath(getApplicationContext()),"test");
if(myDir.mkdirs()){
Log.i(TAG, "Directory was succesfully create!");
}else{
Log.i(TAG, "Error creating directory!");
}
For example using the method:
String pathSDCard = getRemovableSDCardPath(getApplicationContext());
I have as a result the path of my removable SD Card (if i wouldn´t have a removable SD Card my path would be "", so you can implemente a validation to avoid the creation of the folder):
/storage/extSdCard/Android/data/com.jorgesys.myapplication/files
Now creating a new folder inside :
File myDir = new File(getRemovableSDCardPath(getApplicationContext()),"test");
if(myDir.mkdirs()){
Log.i(TAG, "Directory was succesfully create!");
}else{
Log.i(TAG, "Error creating directory!");
}
now i have the directory /test
created:
这篇关于强制访问外部可移动microSD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!