我想创建新文件和写/读应用程序设置。 isExternalStorageAvailable()返回false并且应该为true。 Android模拟器外部存储器= 200mb。
boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
添加了对清单文件的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveButton.setEnabled(false);
System.out.println("Available: "+isExternalStorageAvailable()+", ReadOnly: "+isExternalStorageReadOnly());
}
else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
}
答案 0 :(得分:0)
要获取所有外部存储路径,可以使用:
File[] extSDCardPaths = null;
try {
extSDCardPaths = ContextCompat.getExternalFilesDirs(ctx, null);
} catch (Exception e) {
// handle
}
for (File f : extSDCardPaths) {
if (Environment.isExternalStorageRemovable(f)) {
// check if anything found
}
}
如果extSDCardPaths
返回某些内容,则可以使用ext存储。
以上适用于&gt; 4.4(KitKat)仅
以下hack可用作较低操作系统版本的后备:
String[] fallback = {"/storage/sdcard1/", "/mnt/ext_card/", "/mnt/extSdCard/", "/storage/extsdcard/",
"/mnt/extsdcard/", "/storage/extSdCard/", "/mnt/external_sd", "/storage/MicroSD/",
"/storage/external_SD/", "/storage/ext_sd/", "/storage/removable/sdcard1/", "/mnt/sdcard/external_sd/",
"/storage/ext_sd/", "/mnt/emmc/", "/data/sdext/", "/sdcard/sd/"};
for (String path : fallback) {
if (new File(path).exists()) {
// found an ext. dir
break;
}
}
答案 1 :(得分:0)
如果您的应用程序定位并在marshmallow或更高版本上运行,您必须获得读取/写入外部存储的权限,您必须在声明清单中的权限后明确请求用户的许可。
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
明确检查权限。如果没有提供,请求:
public void checkPermissionReadStorage(Activity activity){
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
接收权限回复:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PermissionManager.MY_PERMISSIONS_REQUEST_READ_STORAGE:
//premission to read storage
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted. Call your storage related code here
} else {
// permission denied.
// Handle permission failed here.
}
return;
// other 'case' lines to check for other
// permissions this app might request
}
}
代码取自here