package com.rovedashcam.android.view.common.activity;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.rovedashcam.android.R;
import com.rovedashcam.android.view.base.BaseActivity;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class NewUploadPhoto extends BaseActivity {
File directory;
String downloadLink = "https://raw.githubusercontent.com/charly1811/google-books-api-android-sample/master/app/src/main/res/mipmap-xhdpi/ic_launcher.png";
String fileName;
String folderName;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_upload);
folderName = "RoveDashCam/Photos";
createFolderForApp(folderName);
}
private void createFolderForApp(String FolderName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/" + FolderName);
} else {
directory = new File(Environment.getExternalStorageDirectory() + "/" + FolderName);
}
Log.i("TAG", "createFolderForApp: " + directory.getPath());
if (!directory.exists()) {
boolean success = directory.mkdirs();
if (!success) {
directory = null;
} else {
new FileDownloader().execute(downloadLink);
}
} else {
new FileDownloader().execute(downloadLink);
}
}
private class FileDownloader extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog();
}
@Override
protected String doInBackground(String... params) {
String result = "";
if (!TextUtils.isEmpty(downloadLink)) {
String MY_URL = downloadLink;
try {
result = downloadFile(MY_URL);
} catch (Exception e) {
Log.i("TAG", "doInBackground: " + e.getLocalizedMessage());
}
} else {
}
return result;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
hideProgressDialog();
Toast.makeText(NewUploadPhoto.this, result, Toast.LENGTH_SHORT).show();
}
}
private String downloadFile(String MY_URL) {
int count;
try {
URL url = new URL(MY_URL);
URLConnection connection = url.openConnection();
connection.connect();
long startTime = System.currentTimeMillis();
int lengthOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
fileName = MY_URL.substring(MY_URL.lastIndexOf('/') + 1, MY_URL.length());
File outputFile = new File(directory, fileName);
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
int status = (int) ((total * 100) / lengthOfFile);
output.write(data, 0, count);
new Handler().post(() -> {
showProgressDialog();
if (status == 100) {
hideProgressDialog();
}
});
}
output.flush();
output.close();
input.close();
return "Downloaded at: " + directory.getPath() + fileName;
} catch (IOException e) {
Log.e("Abhan", "Error: " + e.getLocalizedMessage());
runOnUiThread(() -> hideProgressDialog());
}
hideProgressDialog();
return "Downloading Error";
}
}
这是我的代码,我试图在给定的文件夹"RoveDashCam/Photos"的SD卡中下载图像,但当我得到错误java. io. FileNotFoundException:/存储/模拟/0/电影/RoveDashCam/照片/ic_launcher. png:打开失败:EPERM(不允许操作)
我已经给出了写外部和图像读取视频读取权限的许可,然后我也得到错误,任何人可以请帮助我在这我做错了这个代码如何我可以下载图像到SDCard在Android。这是只发生在Android 13手机。Android 10我已经检查了我能够下载。
1条答案
按热度按时间ddrv8njm1#
您无法在Android 11+设备上的公共Movies目录中创建图像文件。
只有视频文件。