Android的:如何从网上下载的二进制文件二进制文件、网上、Android

2023-09-06 19:13:35 作者:那一種庝゛撕惢裂肺

我想从互联网上下载的文件。到目前为止我有FF。 codeS:

 包com.example.downloadfile;进口java.io.BufferedOutputStream;进口android.app.Activity;进口android.os.Bundle;进口android.widget.TextView;公共类DownloadFile延伸活动{    / **当第一次创建活动调用。 * /    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);        TextView的电视=新的TextView(本);        字符串URL =htt​​p://www.fullissue.com/wp-content/uploads/2010/12/Adam-Lambert.jpg;        字符串文件名=/ LocalDisk / JM //保存在你​​的SD卡        尝试{            在java.io.BufferedInputStream中=新java.io.BufferedInputStream中(新的java.net.URL(URL).openStream());            java.io.FileOutputStream中的FOS =新java.io.FileOutputStream中(文件名);            java.io.BufferedOutputStream回合=新的BufferedOutputStream(FOS,1024);            字节[]数据=新的字节[1024];                INT X = 0;                而((X = in.read(数据,0,1024))GT; = 0){                    bout.write(数据,0,x)的;                }            fos.flush();            bout.flush();            fos.close();            bout.close();            附寄();        }赶上(例外五){            / *显示任何错误的GUI。 * /            tv.setText(错误:+ e.getMessage());        }      this.setContentView(电视);    }} 

我得到一个错误,当我运行这个code,我得到了错误:/ LocalDisk / JM 在我的UI

在您的帮助非常感谢!我是新来的Java和Android开发...:)

解决方案   

字符串文件名=/ LocalDisk / JM;

修改Android apk的二进制文件AndroidManifest.xml,并重新签名打包apk

这是绝对路径,所以你必须使用这样

 字符串文件名= Environment.getExternalStorageDirectory()+/ JM // / SD卡/ JM 

有关更多信息 getExternalStorageDirectory()。也有是下载一个文件UI线程的原因,而是阻塞操作,可能会导致活动不响应任何好主意。最好的办法,是使用的AsyncTask 来处理这个背景和操作当你完成通知GUI。

I want to download a file from the internet. And so far I have the ff. codes:

package com.example.downloadfile;

import java.io.BufferedOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class DownloadFile extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = new TextView(this);

        String url = "http://www.fullissue.com/wp-content/uploads/2010/12/Adam-Lambert.jpg";
        String FileName = "/LocalDisk/jm"; // save in your sdcard


        try{

            java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(url).openStream());
            java.io.FileOutputStream fos = new java.io.FileOutputStream(FileName);
            java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
            byte[] data = new byte[1024];
                int x=0;
                while((x=in.read(data,0,1024))>=0){
                    bout.write(data,0,x);               
                }
            fos.flush();
            bout.flush();
            fos.close();
            bout.close();
            in.close();

        }catch (Exception e){
            /* Display any Error to the GUI. */
            tv.setText("Error: " + e.getMessage());
        }

      this.setContentView(tv);
    }
}

I'm getting an error, when I run this code, I got "Error: /LocalDisk/jm" in my UI.

Many thanks in advance for any help! I'm new to java and android dev... :)

解决方案

String FileName = "/LocalDisk/jm";

This is absolute path so you have to use it like this

String FileName = Environment.getExternalStorageDirectory() + "/jm"; // /sdcard/jm

More info about getExternalStorageDirectory(). Also there is no good idea to download a file in UI thread cause it's blocking operation and may cause Activity Not Responding. The best way will be to use AsyncTask to handle this operation in background and notify the gui when you're done.