我是android app开发者新手!
我有一个新问题。
我在我的MainActivity类中有这个void,一旦用户点击一个复选框就调用它,它的作用基本上就是将web上的文件下载到外部存储中名为test的文件夹中我在AndroidManifest.xml中添加了nessacry权限(有没有办法将文件下载到内部存储器),但它在运行时给出一个fc是日志的一部分......
LOG
D/DownloadManager(14956): download begining
D/DownloadManager(14956): download url:http://forum.xda-developers.com/attachment.php?attachmentid=2109443&d=1373572809l
D/DownloadManager(14956): downloaded file name:X-Reality_Engine.zipA
D/AndroidRuntime(14956): Shutting down VM
W/dalvikvm(14956): threadid=1: thread exiting with uncaught exception (group=0x4
1dc0ce0)
I/Process (14956): Sending signal. PID: 14956 SIG: 9
I/ActivityManager(23309): Process com.mythi.tests (pid 14956) has died.
MainActivity.java
public void onCheckboxClicked5(View view) {
String DownloadUrl = "http://forum.xda-developers.com/attachment.php?attachmentid=2109443&d=1373572809";
String fileName = "X-Reality_Engine.zip";
// Is the view now checked?
boolean checked5 = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkBox5:
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putBoolean("checkBox5", checked5).commit();
if (checked5){
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/test");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "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(5000);
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.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
答案 0 :(得分:0)
在您的日志中没有说,但我发现您没有使用单独的线程进行网络访问。这是无效的情况,您的应用程序将崩溃。您必须创建一个单独的线程才能从Web下载该文件。 SEE THIS Android docs
答案 1 :(得分:0)
将此代码添加到onCLick按钮,或者您点击下载的任何按钮或行
public void downloadFile(){
String DownloadUrl = "Paste Url to download a pdf file here…";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));
request.setDescription("sample pdf file for testing"); //appears the same in Notification bar while downloading
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalFilesDir(getApplicationContext(),null, "whatevernameoffileuwant.extensionoffilw");
//request.setDestinationInExternalFilesDir(getApplicationContext(),null, "Sample.pdf");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}