有没有在Android 8.0+ sdcard上编写的解决方案?目前正在尝试使用DocumentFile。(https://github.com/TeamAmaze/AmazeFileManager/blob/master/app/src/main/java/com/amaze/filemanager/filesystem/HybridFile.java)
请问大家还有什么方式能在Android 8.0+ sdcard上面做写入的动作吗?
private final static String localFullPath = PATH_SDCARD + File.separator + nowFormat + "_log.txt";
public static void logWriter(String logText) {
try {
File logFile = new File(localFullPath);
if (!logFile.exists()) {
if (logFile.createNewFile()) {
Log.d("mkdir", "Create new file: " + localFullPath);
}
}
Date date = new Date(System.currentTimeMillis());
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
String nowFormat = simpleDateFormat.format(date);
FileWriter fileWriter = new FileWriter(localFullPath, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.append("[").append(nowFormat).append("] ").append(logText);
bufferedWriter.newLine();
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void CheckPermission() {
// CheckStoragePermission();
String PERMISSION_WRITE_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";
String PERMISSION_READ_PHONE_STATE = "android.permission.READ_PHONE_STATE";
String PERMISSION_ACCESS_FINE_LOCATION = "android.permission.ACCESS_FINE_LOCATION";
String PERMISSION_ACCESS_COARSE_LOCATION = "android.permission.ACCESS_COARSE_LOCATION";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((ContextCompat.checkSelfPermission(this, PERMISSION_WRITE_STORAGE) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, PERMISSION_ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, PERMISSION_ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, PERMISSION_READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)) {
String[] perms = {PERMISSION_WRITE_STORAGE, PERMISSION_READ_PHONE_STATE, PERMISSION_ACCESS_FINE_LOCATION, PERMISSION_ACCESS_COARSE_LOCATION};
int permsRequestCode = 1;
requestPermissions(perms, permsRequestCode);
}
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
public static String[] getExtSdCardPathsForActivity(Context context) {
List <String> paths = new ArrayList <>();
for (File file : context.getExternalFilesDirs("external")) {
if (file != null) {
int index = file.getAbsolutePath().lastIndexOf("/Android/data");
if (index < 0) {
Log.w(LOG, "Unexpected external file dir: " + file.getAbsolutePath());
} else {
String path = file.getAbsolutePath().substring(0, index);
try {
path = new File(path).getCanonicalPath();
} catch (IOException e) {
// Keep non-canonical path.
}
paths.add(path);
}
}
}
if (paths.isEmpty()) paths.add("/storage/sdcard1");
return paths.toArray(new String[0]);
}
1仅在AndroidManifest.xml中定义权限是不够的,您必须在onCreate @MainActivity中requestPermissions。 2另一种解决方法是将sdk版本定位到低于M,对于ex targetSdkVersion 15。
在Android 8+,您还必须添加READ_EXTRERNAL_STORAGE权限
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if ((checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) ||
(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
Log.e(getClass().getSimpleName(), "missing permission: external storage");
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
builder.setTitle(getResources().getString(R.string.assign_permissions));
builder.setMessage(getResources().getString(R.string.permissions_prompt));
builder.setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
requestPermissions(new String[]
{android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE);
}
dialog.dismiss();
}
});
builder.show();
return;
}
}