sách gpt4 ăn đã đi

android - Không thể tải tập tin xuống điện thoại Android - Android..?

In lại Tác giả: Walker 123 更新时间:2023-11-29 22:12:54 30 4
mua khóa gpt4 giày nike

我在下载文件时遇到问题,

我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限

我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但在手机中不工作。

biên tập我的下载文件代码

public void downloadFile(String _url, String fileName) {
File PATH = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
thử {
PATH.mkdirs();
URL url = new URL(_url); // you can write here any link
File file = new File(PATH, fileName);

long startTime = System.currentTimeMillis();
Log.d("Manager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();

/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}

/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("ImageManager",
"download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");

} bắt (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}

câu trả lời hay nhất

试试下面给出的 fragment ...

File PATH = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
thử {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
_url = _url.replace(" ", "%20");
URL url = new URL(_url);

//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);


//and connect!
urlConnection.connect();

//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(PATH,fileName);

//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);

//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();

//this is the total size of the file
int totalSize = urlConnection.getContentLength();
Log.i("Download", totalSize+"");
//variable to store total downloaded bytes
// int downloadedSize = 0;

//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer

//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
}
//close the output stream when done
fileOutput.close();
trả về giá trị đúng;

//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} bắt (IOException e) {
e.printStackTrace();
}

确保您输入了正确的下载路径(url)

关于android - 无法在 Android 手机中下载文件 - Android..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9208103/

30 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress