Anas.J:
私は、SDカード上のいくつかのファイルを削除しようとしていますが、私はアイデアを使い果たしました..
私が試したFile.delete()
、方法を、私は試してみましたがfile.getCanonicalFile().delete()
、多くの詳細..私のアプリケーションことができ、デバイスのストレージ上だけでファイルの削除。私は以下のように必要とされるマニフェストファイルで定義された権限を持っています。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
そして私はまた、コード内で許可を要求しています:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(this)
.setTitle(R.string.read_storage_message)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MainActivity.READ_STORAGE_PERMISSION_ID);
}
}
)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AppKiller.kill();
}
}
).show();
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(this)
.setTitle(R.string.write_storage_message)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MainActivity.STORAGE_PERMISSION_ID);
}
}
)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AppKiller.kill();
}
}
).show();
}
}
私は、ファイルを削除するには、次の方法を使用していますが、私は前に述べたように、私は、スタックオーバーフローなどで見つかったソリューションの使用をたくさんしました。
protected boolean delete(Context context) throws Exception {
boolean delete = false;
File file = new File(Environment.getExternalStorageDirectory().getPath() + getName());
if (file.exists()) {
delete = file.delete();
if (!delete) {
delete = file.getCanonicalFile().delete();
}
if (!delete) {
String deleteCmd = "rm -r " + file.getAbsolutePath();
Runtime runtime = Runtime.getRuntime();
runtime.exec(deleteCmd);
}
}
return delete;
}
私が求めていますので、それは可能性READ_EXTERNAL_STORAGE
とWRITE_EXTERNAL_STORAGE
権限が、コードでは、私はちょうど取得していますREAD_EXTERNAL_STORAGE
許可され、その他は、Android(それが許可表示されませんから無視なっているWRITE_EXTERNAL_STORAGE
私は最初のものを受け入れた後、許可、拒否のオプションでポップアップを)。彼らはアンドロイドで同じ権限のレベルを持っているので、それはないですか?
何が間違っているだろうか?
greenapps:
はい、あなたはもうFileクラスを使用して、現代のAndroidのバージョンでSDカードからファイルを削除することはできません。
代わりに、ストレージアクセスのフレームワークを使用してください。
見ているIntent.OPEN_DOCUMENT_FILE
とOPEN_DOCUMENT_TREE
。