安卓开发 实现下载文件功能

博客涉及Android、HTML和JavaScript相关信息技术内容,但具体内容缺失。推测可能围绕Android开发中HTML与JavaScript的应用等方面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      /**
        * AsyncHttpClient 是自定义的一个工具类 用于存放downloadStream下载方法的
        *  总体思路就是 先得到手机的一个目录 然后建一个文件夹 往里写数据流
        **/

       AsyncHttpClient client= new AsyncHttpClient();
         /**
           *创建本地文件   CommonUtil是我存放方法的地方 getRootFilePath方法我放到下面了 
           *自己 找地方放 然后把CommonUtil改成自己的 
           **/
            String pPath = CommonUtil.getRootFilePath();

            //DATA_CACHE_PATH自己写的目录
            String fileDir = pPath + Constants.DATA_CACHE_PATH;
            File file = new File(fileDir);
            if (!file.exists()) {
                file.mkdir();
            }
            String s = orderID+mDatalist.get(position).getFileName();

            File f = new File(file.getAbsolutePath(), s);
            if (!f.exists()) {
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           // client.downloadFile(url,null,s,AttachmentActivity.this);
           client.downloadStream(url,f.getAbsolutePath(),null);

这上面是使用的地方用的代码 下面的代码找个地方放就可以  是具体的方法
//这个就是下载数据流的方法了
public boolean downloadStream(String url, String filePath, String content) {
		URL u = null;
		boolean rtn = false;
		FileOutputStream output = null;
		InputStream is = null;
		try {
			u = new URL(url);

			HttpURLConnection connection = null;

			connection = (HttpURLConnection) u.openConnection();
			connection.setRequestProperty("connection", "Keep-Alive");
			// connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			// connection.setRequestProperty("Content-Type",
			// "application/json");
			// connection.setDoInput(true);
			connection.setReadTimeout(5000);
			connection.setConnectTimeout(5000);
			// connection.setDoOutput(true);
			connection.connect();
			if (!TextUtils.isEmpty(content)) {
				OutputStream outputStream = connection.getOutputStream();
				outputStream.write(content.getBytes());
				outputStream.close();
			}
			File wdFile = new File(filePath);
			output = new FileOutputStream(wdFile);
			is = connection.getInputStream();
			connection.getContentLength();

			byte data[] = new byte[1024];
			int count = 0;
			while ((count = is.read(data)) != -1) {
				output.write(data, 0, count);
			}
			output.flush();
			rtn = true;
		}

		catch (Exception e) {
			rtn = false;
			e.printStackTrace();
		}

		finally {
			try {
				if (is == null) {

					return false;
				} else {
					output.close();
					is.close();
				}
			} catch (IOException e) {

				e.printStackTrace();
			}

		}
		return rtn;

	}
//找到手机中内置存储卡的根目录
public static String getRootFilePath() {
		if (hasSDCard()) {
			return Environment.getExternalStorageDirectory().getAbsolutePath()
					+ "";
		} else {
			return Environment.getDataDirectory().getAbsolutePath() + "/data";
		}
	}


//上面需要判断用的方法
public static boolean hasSDCard() {
		String status = Environment.getExternalStorageState();
		if (!status.equals(Environment.MEDIA_MOUNTED)) {
			return false;
		}
		return true;
	}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值