赞
踩
要解决在外部存储,挂载地址为/mnt/media_rw/B85B-FF8D下无法创建文件问题
6.0、7.0可用,8.0以上未测。
最后创建成功地址为/storage/B85B-FF8D/Android/data/com.xxb.test/files/1.txt
第一、静态权限问题。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxb.test" > + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> + <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/logo" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
第二、动态权限问题
摘自:https://blog.csdn.net/LosingCarryJie/article/details/80889154
原谅我的搬砖行为。
private static final int NOT_NOTICE = 2;//如果勾选了不再询问 private AlertDialog alertDialog; private AlertDialog mDialog; private void myRequetPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); }else { Toast.makeText(this,"您已经申请了权限!",Toast.LENGTH_SHORT).show(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 1) { for (int i = 0; i < permissions.length; i++) { if (grantResults[i] == PERMISSION_GRANTED) {//选择了“始终允许” Toast.makeText(this, "" + "权限" + permissions[i] + "申请成功", Toast.LENGTH_SHORT).show(); } else { if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {//用户选择了禁止不再询问 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("permission") .setMessage("点击允许才可以使用我们的app哦") .setPositiveButton("去允许", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if (mDialog != null && mDialog.isShowing()) { mDialog.dismiss(); } Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", getPackageName(), null);//注意就是"package",不用改成自己的包名 intent.setData(uri); startActivityForResult(intent, NOT_NOTICE); } }); mDialog = builder.create(); mDialog.setCanceledOnTouchOutside(false); mDialog.show(); } else {//选择禁止 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("permission") .setMessage("点击允许才可以使用我们的app哦") .setPositiveButton("去允许", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if (alertDialog != null && alertDialog.isShowing()) { alertDialog.dismiss(); } ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } }); alertDialog = builder.create(); alertDialog.setCanceledOnTouchOutside(false); alertDialog.show(); } } } } }
第三步、比较坑的一步。私有化。
摘自:https://blog.csdn.net/marketandtechnology/article/details/73275284
public void MakedirFile() {
this.getExternalFilesDir(null).getAbsolutePath();//这一步重要
File file = new File(getStoragePath(this,true)+"/Android/data/"+this.getPackageName()+"/files/1.txt");
try {
file.createNewFile();
Log.i(TAG, "创建成功 -1 "+file);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "创建失败 -2 " +file);
}
}
其他、U盘地址,
摘自:https://blog.csdn.net/u010838555/article/details/51783384
private static String getStoragePath(Context mContext, boolean is_removale) { StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); Class<?> storageVolumeClazz = null; try { storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); Method getPath = storageVolumeClazz.getMethod("getPath"); Method isRemovable = storageVolumeClazz.getMethod("isRemovable"); Object result = getVolumeList.invoke(mStorageManager); final int length = Array.getLength(result); for (int i = 0; i < length; i++) { Object storageVolumeElement = Array.get(result, i); String path = (String) getPath.invoke(storageVolumeElement); boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement); if (is_removale == removable) { return path; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。