gpt4 book ai didi

android - 我向 list 文件添加了写入外部存储权限,尽管 logcat 说没有写入权限。如何解决

转载 作者:行者123 更新时间:2023-11-30 01:30:26 26 4
gpt4 key购买 nike

这是我的 Logcat 消息

                                             java.lang.SecurityException: No permission to write to /storage/0F0D-0A0C/Download/notice.php: Neither user 10082 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.

我正在尝试从外部存储下载文件,我给出了文件的路径,我的下载文件代码是

TextView downloadlink = (TextView)findViewById(R.id.downloadlink);
downloadlink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myHTTPUrl = "http://192.168.122.1/notice.php";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myHTTPUrl));
request.setTitle("File download");
request.setDescription("File is being downloaded...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String nameOfFile = URLUtil.guessFileName(myHTTPUrl,null, MimeTypeMap.getFileExtensionFromUrl(myHTTPUrl));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameOfFile);
DownloadManager manager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
});

list 文件权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>

最佳答案

请检查您的应用程序当前是否在 MarshMallow 或更高版本中运行,然后需要检查运行时的危险权限。请将其包含在 MainActivity 中。你可以从任何地方调用它。

public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;

public boolean checkPermission()
{
int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
android.support.v7.app.AlertDialog.Builder alertBuilder = new android.support.v7.app.AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("To download a file it is necessary to allow required permission");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity)context, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
});
android.support.v7.app.AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity)context, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}

这里检查权限是否已经授予,如果应用程序当前运行在 marshmallow 或更高版本中,则检查它是否为。我在这里更改了您的代码。

TextView downloadlink = (TextView)findViewById(R.id.downloadlink);
downloadlink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkPermission()){

downloadFile();
}
}
});

您的下载代码放在一个方法中

downloadFile(){
String myHTTPUrl = "http://192.168.122.1/notice.php";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myHTTPUrl));
request.setTitle("File download");
request.setDescription("File is being downloaded...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String nameOfFile = URLUtil.guessFileName(myHTTPUrl,null, MimeTypeMap.getFileExtensionFromUrl(myHTTPUrl));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameOfFile);
DownloadManager manager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}

如果已授予权限,则下载代码将正常工作,否则它会调用覆盖方法“onRequestPermissionsResult()”

下面给出覆盖方法

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
downloadFile();
} else {
//code for deny
}
break;
}
}

请试试这个,然后它会起作用

关于android - 我向 list 文件添加了写入外部存储权限,尽管 logcat 说没有写入权限。如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com