Android 本地路径

来源:互联网 发布:java页面导出excel 编辑:程序博客网 时间:2024/06/05 14:33

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();}}}



0 0
原创粉丝点击