欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 文件下载到SDCARD 博客分类: android android文件下载 

程序员文章站 2024-03-14 22:35:29
...

操作SDcard 需要权限

//写入数据权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//创建和删除文件权限
 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 

 主界面

package com.example.loaddown;

import java.io.File;

import util.DownUtil;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Activity08 extends Activity {
	
	EditText t1 ;
	EditText t2 ;
	Spinner sp ;
	Button b1 ;
	String name = null ;
	ArrayAdapter ad = null ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity08);
        
        t1 = (EditText) findViewById(R.id.t1);
        t2 = (EditText) findViewById(R.id.t2);
        
        b1 = (Button) findViewById(R.id.b1);
        b1.setText("下载");
        
        sp = (Spinner) findViewById(R.id.spin);
        ad = ArrayAdapter.createFromResource(this, R.array.spin, android.R.layout.simple_spinner_item);
        ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setPrompt("文件格式");
        sp.setAdapter(ad);
        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				name = ad.getItem(arg2).toString();
				System.out.println(name);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		}) ;
        
        
        b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			String loadpath = t1.getText().toString();
			String fileName = t2.getText().toString();
			DownUtil down = new DownUtil();
			File file = new File(Environment.getExternalStorageDirectory()+"/"+fileName+name);
			Intent intent = new Intent() ;
			if(file.exists()){
				intent.putExtra("message", "文件已经存在");
			}
			else{
			down.loadTxt(fileName+name, loadpath);
			intent.putExtra("message", "下载完成");
			}
			
			intent.setClass(Activity08.this, OverActivity.class);
			Activity08.this.startActivity(intent);
			
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_activity08, menu);
        return true;
    }
}


 

结果界面:

package com.example.loaddown;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class OverActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.over);
		TextView text = (TextView) findViewById(R.id.text1);
		Intent intent = getIntent();
		String msg = intent.getStringExtra("message");
		text.setText(msg);
		
	}

	
	
}

  

 

工具类:

package util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Environment;

public class DownUtil {
	private URL url = null ;
	private HttpURLConnection con = null ; 
	String filePath ;

	
	public File down(String fileName,String loadPath){
		
			filePath = Environment.getExternalStorageDirectory()+"/";
			System.out.println(filePath);
			File file = new File(filePath+fileName);
		try {
			url = new URL(loadPath);
			con = (HttpURLConnection) url.openConnection() ;
			InputStream in = con.getInputStream() ;
			BufferedInputStream bfin = new BufferedInputStream(in);
			BufferedOutputStream bfout = new BufferedOutputStream(new FileOutputStream(file));
			int k = 0;
			byte b[] = new byte[4*1024];
			while((k=bfin.read(b))!=-1){
				bfout.write(b);
			}
			
			bfout.flush();
			bfout.close();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return file;
		
	}
	
	
	public File loadTxt(String fileName,String loadPath){
		
		filePath = Environment.getExternalStorageDirectory()+"/";
		System.out.println(filePath);
		File file = new File(filePath+fileName);
		try {
			url = new URL(loadPath);
			con = (HttpURLConnection) url.openConnection() ;
			InputStream in = con.getInputStream() ;
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			BufferedWriter writer = new BufferedWriter(new FileWriter(file));
			String s = null ;
			while ((s= reader.readLine())!=null){
				writer.write(s);
			}			
			writer.flush();
			writer.close();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return file;

	}
	
	
}