admin管理员组文章数量:1540805
2023年12月13日发(作者:)
Android sdk 3.0 编译通过
package ad;
import ;
import tputStream;
import ption;
import tream;
import ;
import nection;
import ProtocolException;
import ty;
import ;
import nment;
import r;
import e;
import ;
import ;
import kListener;
import ;
import ssBar;
import ew;
import ;
public class AndroidDownLoadActivity extends Activity
{
private Button DownloadTextButton = null;
private Button DownloadMp3Button = null;
ProgressBar pb;
TextView tv;
int fileSize;
int downLoadFileSize;
String fileEx, fileNa, filename;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{// 定义一个Handler,用于处理下载线程与UI间通讯
if (!tThread().isInterrupted())
{
};
}
switch () {
case 0:
(fileSize);
case 1:
gress(downLoadFileSize);
int result = downLoadFileSize * 100 / fileSize;
t(result + "%");
break;
case 2:
xt(, "文件下载完成", 1)
.show();
break;
case -1:
String error = a().getString("error");
xt(, error, 1)
.show();
break;
}
}
Message(msg);
/*
* down load file to sdcard.
*/
public void down_file(String url, String path) throws IOException
{
// 下载函数
filename = ing(dexOf("/") + 1);
// 获取文件名
URL myURL = new URL(url);
URLConnection conn = nnection();
t();
InputStream is = utStream();
ze = tentLength();// 根据响应获取文件大小
Log.i("value", "length = " + ze);
if (ze <= 0)
{
throw new RuntimeException("无法获知文件大小 ");
}
if (is == null)
{
}
}
throw new RuntimeException("stream is null");
File file = new File(ernalStorageDirectory(),
filename);
FileOutputStream fos = new FileOutputStream(file);
// 把数据存入路径+文件名
byte buf[] = new byte[1024];
downLoadFileSize = 0;
sendMsg(0);
do
{
// 循环读取
int numread = (buf);
if (numread == -1)
{
break;
}
(buf, 0, numread);
downLoadFileSize += numread;
sendMsg(1);// 更新进度条
} while (true);
sendMsg(2);// 通知下载完成
try
{
();
} catch (Exception ex)
{
Log.e("tag", "error: " + sage(), ex);
}
private void sendMsg(int flag)
{
Message msg = new Message();
= flag;
ssage(msg);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
te(savedInstanceState);
setContentView();
}
private class DownTextButtonListener implements OnClickListener
{
public void onClick(View v)
{
te(savedInstanceState);
setContentView();
pb = (ProgressBar) findViewById(_pb);
tv = (TextView) findViewById();
new Thread()
{
public void run()
{
try
{
down_file(
}
}.start();
"下载路径 ",
"/sdcard/");
// 下载文件,参数:第一个URL,第二个存放路径
} catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
tackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
tackTrace();
}
DownloadTextButton = (Button) findViewById(xtButtonId);
lickListener(new DownTextButtonListener());
DownloadMp3Button = (Button) findViewById(3ButtonId);
lickListener(new DownMp3ButtonListener());
}
}
}
Thread thread = new Thread(new Runnable()
{
public void run()
{
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
String irc = httpDownloader
.download("下载路径 ",);
n(irc);
}
});
();
private class DownMp3ButtonListener implements OnClickListener
{
public void onClick(View v)
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader
.downFile(
}
}
"下载路径 ",,
"test/", "");
n(result);
}
});
();
package ad;
import edReader;
import ;
import ption;
import tream;
import treamReader;
import LConnection;
import medURLException;
import ;
import nection;
import ;
public class HttpDownloader
{
private URL url = null;
public String download(String urlStr)
{
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try
{
// 创建一个url对象
url = new URL(urlStr);
// 创建一个Http连接对象
//HttpURLConnection urlConn = (HttpURLConnection) nnection();
URLConnection urlConn = nnection();
t();
int fileSize = tentLength();// 根据响应获取文件大小
Log.d("filesize " ," **** " + fileSize);
// 使用IO流读取数据
buffer = new BufferedReader(new InputStreamReader(urlConn
}
.getInputStream()));
while ((line = ne()) != null)
{
(line);
}
} catch (Exception e)
{
tackTrace();
} finally
{
try
{
();
} catch (Exception e)
{
tackTrace();
}
}
return ng();
public int downFile(String urlStr, String path, String fileName)
{
InputStream inputStream = null;
try
{
FileUtils fileUtils = new FileUtils();
if (Exist(path + fileName))
{
return 1;
} else
{
inputStream = getInputStreamFromUrl(urlStr);
File resultFile = 2SDFromInput(path, fileName,
inputStream);
if (resultFile == null)
{
return -1;
}
} } catch (Exception e)
{
tackTrace();
return -1;
} finally
{
try
{
();
} catch (Exception e)
{
tackTrace();
}
}
return 0;
}
private InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException, IOException
{
// TODO Auto-generated method stub
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) nnection();
InputStream inputStream = utStream();
return inputStream;
}
}
package ad;
import ;
import tputStream;
import ption;
import tream;
import Stream;
import nment;
public class FileUtils
{
private String SDPATH;
public String getSDPATH()
{
return SDPATH;
}
public FileUtils()
{
SDPATH = ernalStorageDirectory() + "/";
}
public File createSDFile(String filename) throws IOException
{
File file = new File(SDPATH + filename);
NewFile();
return file;
}
public File createSDDir(String dirname) throws IOException
{
File file = new File(SDPATH + dirname);
();
return file;
}
public boolean isFileExist(String fileName)
{
File file = new File(SDPATH + fileName);
return ();
}
public File write2SDFromInput(String path, String fileName,
InputStream input)
{
File file = null;
OutputStream output = null;
try
{
createSDDir(path);
file = createSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024]; while (((buffer)) != -1)
{
(buffer);
}
();
} catch (Exception e)
{
tackTrace();
} finally
{
try
{
();
} catch (Exception e)
{
tackTrace();
}
}
return file;
}
}
xmlns:android="/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" style="?android:attr/progressBarStyleHorizontal" />
package="ad" android:versionCode="1" android:versionName="1.0"> android:name="_EXTERNAL_STORAGE"> android:name="_UNMOUNT_FILESYSTEMS"> android:name="_OWNER_DATA"> android:name="_OWNER_DATA"> android:label="@string/app_name"> android:label="@string/app_name"> android:name="ER" /> android:name="_EXTERNAL_STORAGE"/>
版权声明:本文标题:android 文件下载及进度条 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1702457645a7936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论