我正试图在android中使用异步任务下载和存储图像,当点击下载按钮时得到以下错误。
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/downloadedfilem.jpg (Permission denied)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at java.io.FileOutputStream.open(Native Method)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:108)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at myoracle.com.quotes.WallpaperDeatilsActivity$ImageDownload.doInBackground(WallpaperDeatilsActivity.java:113)
12-07 11:01:00.120 28478-28540/myoracle.com.quotes W/System.err: at myoracle.com.quotes.WallpaperDeatilsActivity$ImageDownload.doInBackground(WallpaperDeatilsActivity.java:91)
class ImageDownload extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
int count;
try {
String root = Environment.getExternalStorageDirectory().toString();
System.out.println("Downloading");
URL url = new URL(params[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
System.out.println(root);
OutputStream output = new FileOutputStream(root+"/downloadedfilem.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
System.out.println(count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
Android.permission.write_external_storage
您需要添加OSMarshmallow或以上版本的运行时权限。添加此代码是为了允许运行时操作在splash活动中,在onCreate中或在图像下载过程之前。
if (!checkPermission()) {
openActivity();
} else {
if (checkPermission()) {
requestPermissionAndContinue();
} else {
openActivity();
}
}
在oncreate
外添加此方法
private static final int PERMISSION_REQUEST_CODE = 200;
private boolean checkPermission() {
return ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
;
}
private void requestPermissionAndContinue() {
if (ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, WRITE_EXTERNAL_STORAGE)
&& ActivityCompat.shouldShowRequestPermissionRationale(this, READ_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle(getString(R.string.permission_necessary));
alertBuilder.setMessage(R.string.storage_permission_is_encessary_to_wrote_event);
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(SplashActivity.this, new String[]{WRITE_EXTERNAL_STORAGE
, READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
Log.e("", "permission denied, show dialog");
} else {
ActivityCompat.requestPermissions(SplashActivity.this, new String[]{WRITE_EXTERNAL_STORAGE,
READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
} else {
openActivity();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (permissions.length > 0 && grantResults.length > 0) {
boolean flag = true;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
flag = false;
}
}
if (flag) {
openActivity();
} else {
finish();
}
} else {
finish();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void openActivity() {
//add your further process after giving permission or to download images from remote server.
}
问题内容: 我正在尝试使用android中的异步任务下载和存储图像,当单击下载按钮时出现以下错误。 WallpaperActivity.java Android目标Api 7.1.1 清单中添加的权限 android.permission.READ_EXTERNAL_STORAGE android.permission.WRITE_EXTERNAL_STORAGE 问题答案: 您需要为OS Mar
问题内容: 我一直在尝试加密文件,并将这些文件写回到同一位置。但是我收到错误消息说 我的档案是这个 我认为我已经提供了正确的许可。我用来加密文件的代码就是这个。 我在按钮内使用了这种方法 仍然我无法配置此错误。请有人帮忙! 问题答案: 我怀疑您运行的是Android 6.0棉花糖(API 23)或更高版本。在这种情况下, 必须先 实现运行时权限,然后才能尝试读取/写入外部存储。
我正在尝试一个非常基本的相机应用程序的源代码在Android Studio 3.4.1与Android虚拟设备(AVD)Nexus 7(2012)API 29.我用模拟器中的相机拍了一张照片,并试图将其保存为 /storage/emulated/0/Pictures/myPic.png,但 /storage/emulated/0/Pictures/myPic.png打开失败:EACCES(拒绝许可
我尝试使用以下方法从URI获取路径: 当我尝试压缩位图时: 我得到这个错误:
通常,从图库中选择一个视频并用库剪切,问题是我需要将剪切的视频(它存储在外部存储器:/storage/emulated/0/test_video.mp4中)转换为base64,这就是它失败的地方。我该怎么解决呢?我已经将权限放在清单上,并以编程方式请求这些权限。 而我在logcast中得到的只有: