Android下载文件
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity {
?
??? private TextView mTextView01;
??? private EditText mEditText01;
??? private Button mButton01;
??? private static final String TAG = "DOWNLOADAPK";
??? private String currentFilePath = "";
??? private String currentTempFilePath = "";
??? private String strURL="";
??? private String fileEx="";
??? private String fileNa="";
???
??? public void onCreate(Bundle savedInstanceState)
??? {
????? super.onCreate(savedInstanceState);
????? setContentView(R.layout.main);
??????
????? mTextView01 = (TextView)findViewById(R.id.myTextView1);
????? mButton01 = (Button)findViewById(R.id.myButton1);
????? mEditText01 =(EditText)findViewById(R.id.myEditText1);
??
????? mButton01.setOnClickListener(new Button.OnClickListener()
????? {
??????? public void onClick(View v)
??????? {
????????? /* 文件會(huì)下載至local端 */
????????? mTextView01.setText("下載中...");
????????? strURL = mEditText01.getText().toString();
????????? /*取得欲安裝程序之文件名稱*/
????????? fileEx = strURL.substring(strURL.lastIndexOf(".")
????????? +1,strURL.length()).toLowerCase();
????????? fileNa = strURL.substring(strURL.lastIndexOf("/")
????????? +1,strURL.lastIndexOf("."));
????????? getFile(strURL);
???????? }
?????? }
????? );
?????
????? mEditText01.setOnClickListener(new EditText.OnClickListener()
????? {
??????? public void onClick(View arg0){
????????? mEditText01.setText("");
????????? mTextView01.setText("遠(yuǎn)程安裝程序(請(qǐng)輸入U(xiǎn)RL)");
??????? }
????? });
??? }
???
??? /* 處理下載URL文件自定義函數(shù) */
??? private void getFile(final String strPath)? {
????? try
????? {
??????? if (strPath.equals(currentFilePath) )
??????? {
????????? getDataSource(strPath);
??????? }
??????? currentFilePath = strPath;
??????? Runnable r = new Runnable()
??????? {
????????? public void run()
????????? {
??????????? try
??????????? {
????????????? getDataSource(strPath);
??????????? }
??????????? catch (Exception e)
??????????? {
????????????? Log.e(TAG, e.getMessage(), e);
??????????? }
????????? }
??????? };
??????? new Thread(r).start();
????? }
????? catch(Exception e)
????? {
??????? e.printStackTrace();
????? }
??? }
???
???? /*取得遠(yuǎn)程文件*/
??? private void getDataSource(String strPath) throws Exception
??? {
????? if (!URLUtil.isNetworkUrl(strPath))
????? {
??????? mTextView01.setText("錯(cuò)誤的URL");
????? }
????? else
????? {
??????? /*取得URL*/
??????? URL myURL = new URL(strPath);
??????? /*創(chuàng)建連接*/
??????? URLConnection conn = myURL.openConnection();
??????? conn.connect();
??????? /*InputStream 下載文件*/
??????? InputStream is = conn.getInputStream();
??????? if (is == null)
??????? {
????????? throw new RuntimeException("stream is null");
??????? }
??????? /*創(chuàng)建臨時(shí)文件*/
??????? File myTempFile = File.createTempFile(fileNa, "."+fileEx);
??????? /*取得站存盤(pán)案路徑*/
??????? currentTempFilePath = myTempFile.getAbsolutePath();
??????? /*將文件寫(xiě)入暫存盤(pán)*/
??????? FileOutputStream fos = new FileOutputStream(myTempFile);
??????? byte buf[] = new byte[128];
??????? do
??????? {
????????? int numread = is.read(buf);
????????? if (numread <= 0)
????????? {
??????????? break;
????????? }
????????? fos.write(buf, 0, numread);
??????? }while (true);
???????
??????? /*打開(kāi)文件進(jìn)行安裝*/
??????? openFile(myTempFile);
??????? try
??????? {
????????? is.close();
??????? }
??????? catch (Exception ex)
??????? {
????????? Log.e(TAG, "error: " + ex.getMessage(), ex);
??????? }
????? }
??? }
????
??? /* 在手機(jī)上打開(kāi)文件的method */
??? private void openFile(File f)
??? {
????? Intent intent = new Intent();
????? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
????? intent.setAction(android.content.Intent.ACTION_VIEW);
?????
????? /* 調(diào)用getMIMEType()來(lái)取得MimeType */
????? String type = getMIMEType(f);
????? /* 設(shè)置intent的file與MimeType */
????? intent.setDataAndType(Uri.fromFile(f),type);
????? startActivity(intent);
??? }
??? /* 判斷文件MimeType的method */
??? private String getMIMEType(File f)
??? {
????? String type="";
????? String fName=f.getName();
????? /* 取得擴(kuò)展名 */
????? String end=fName.substring(fName.lastIndexOf(".")
????? +1,fName.length()).toLowerCase();
?????
????? /* 依擴(kuò)展名的類(lèi)型決定MimeType */
????? if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")||
????? end.equals("xmf")||end.equals("ogg")||end.equals("wav"))
????? {
??????? type = "audio";
????? }
????? else if(end.equals("3gp")||end.equals("mp4"))
????? {
??????? type = "video";
????? }
????? else if(end.equals("jpg")||end.equals("gif")||end.equals("png")||
????? end.equals("jpeg")||end.equals("bmp"))
????? {
??????? type = "image";
????? }
????? else if(end.equals("apk"))
????? {
??????? /* android.permission.INSTALL_PACKAGES */
??????? type = "application/vnd.android.package-archive";
????? }
????? else
????? {
??????? type="*";
????? }
????? /*如果無(wú)法直接打開(kāi),就跳出軟件列表給用戶選擇 */
????? if(end.equals("apk"))
????? {
????? }
????? else
????? {
??????? type += "/*";?
????? }
????? return type;?
??? }
??? /*自定義刪除文件方法*/
??? private void delFile(String strFileName)
??? {
????? File myFile = new File(strFileName);
????? if(myFile.exists())
????? {
??????? myFile.delete();
????? }
??? }
???
??? /*當(dāng)Activity處于onPause狀態(tài)時(shí),更改TextView文字狀態(tài)*/
??? protected void onPause()
??? {
????? mTextView01 = (TextView)findViewById(R.id.myTextView1);
????? mTextView01.setText("下載成功");
????? super.onPause();
??? }
??? /*當(dāng)Activity處于onResume狀態(tài)時(shí),刪除臨時(shí)文件*/
??? protected void onResume()
??? {?
????? /* 刪除臨時(shí)文件 */
????? delFile(currentTempFilePath);
????? super.onResume();
??? }
}
轉(zhuǎn)載于:https://my.oschina.net/u/614562/blog/228806
總結(jié)
以上是生活随笔為你收集整理的Android下载文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信小程序多人开发-版本管理
- 下一篇: 【树莓派学习笔记】八、两步安装VS Co