からマニフェストからプロセスの下で与えられただけで、他のAndroid OSのpermossion 6.0のために付与されるの権限を削除いけません。 Android 6(API 23)のみを使用しているので、ビルドバージョンを確認する必要はありませんでした。
//
// Get storage write permission
//
public boolean isStoragePermissionGranted(int requestCode) {
if (ContextCompat.checkSelfPermission(MyActivity.this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
//Now you have permission
return true;
} else {
ActivityCompat.requestPermissions(MyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
return false;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Now you have permission
// Check file copy generated the request
// and resume file copy
if(requestCode == WFileRequest)
try {
copyFile(OriginalDataFileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
これは許可例外を通過したものの、既定のアプリケーションディレクトリの外にフォルダを作成する方法には答えられませんでした。次のコードは、アクセス権を取得し、デバイスストレージの最上位に表示されるAppDataと呼ばれる/ storage/emulated/0にフォルダを作成します。アクセス要求コードWFileRequestはAdeel Turkの例のように4に設定されていましたが、任意の番号を使用できることを理解しています。次に、許可要求コールバックは要求コードをチェックし、最初に書き込まれたファイルの名前でcopyFileルーチンを再度呼び出します。
は、このコードのほとんどはhow to create a folder in android External Storage Directory?でこのフォーラムではカップルの他の記事からの例を使用してHow to copy programmatically a file to another directory?
public void copyFile(String SourceFileName) throws FileNotFoundException, IOException
{
String filepath = "";
//
// Check permission has been granted
//
if (isStoragePermissionGranted(WFileRequest)) {
//
// Make the AppData folder if it's not already there
//
File Directory = new File(Environment.getExternalStorageDirectory() + "/AppData");
Directory.mkdirs();
Log.d(Constants.TAG, "Directory location: " + Directory.toString());
//
// Copy the file to the AppData folder
// File name remains the same as the source file name
//
File sourceLocation = new File(getExternalFilesDir(filepath),SourceFileName);
File targetLocation = new File(Environment.getExternalStorageDirectory() + "/AppData/" + SourceFileName);
Log.d(Constants.TAG, "Target location: " + targetLocation.toString());
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
出典
2016-11-12 22:48:17
Max
マシュマロは、ランタイムチェックイン時にパーミッションをチェックする必要があり、このhttps://developer.android.com/training/permissions /requesting.html – Nakul