Android 本地路径

sd卡:Environment.getExternalStorageDirectory().getPath()/自己的文件目录

app安装的目录:this.getFilesDir() + File.separator + "文件夹名/" + fileName

  :  /data/data/包名/files/自己的文件夹名/文件名

手机内存的地址:"/storage/emulated/0/downloadimages/" + fileName


下载图片直接写入本地或者获取到bitmap:

private void getImage(String path) {
		URL url;
		InputStream in = null;
		FileOutputStream fileOutputStream = null;

		String fileName = getFileName(path);
		File parentFile = new File(getParentFilePath());
		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}
		File file = new File(parentFile, fileName);

		try {
			url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5 * 1000);
			conn.setRequestMethod("GET");
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				in = conn.getInputStream();
				// bitmap = BitmapFactory.decodeStream(in);

				fileOutputStream = new FileOutputStream(file);
				byte buffer[] = new byte[1024 * 4];
				int temp = 0;
				while ((temp = in.read(buffer)) != -1) {
					fileOutputStream.write(buffer, 0, temp);
				}

			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}




保存图片到本地的一种方法:

public void saveImage(Bitmap bitmap, String fileName) {

		File parentFile = new File(getParentFilePath());
		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}
		File file = new File(parentFile, fileName);

		FileOutputStream fileOutputStream = null;
		try {

			fileOutputStream = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值