Android 图片在SD卡及包下的存储
public class FileBitmap {
/**
* 获取sd卡中的bitmap,bitmap可见
*
* @param bitmap
* 读取bitmap的路径
* @return bitmap
*/
public static Bitmap getBitmapByPath(String fileNameString, String bitmapURL) {
String bitmapName = bitmapURL.substring(bitmapURL.lastIndexOf("/") + 1);
fileNameString = fileNameString + "/" + bitmapName;
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+fileNameString, options);
return bm;
}
/**
* 在SD卡中存储bitmap,bitmap可见
*
* @param fileName
* 保存bitmap的文件夹路径
* @param bitName
* bitmap的路径
* @param mBitmap
* 要保存的bitmap
* @throws IOException
*/
public static void saveMyBitmap(String fileName, String bitmapURL,
Bitmap mBitmap) throws IOException {
String bitmapName = bitmapURL.substring(bitmapURL.lastIndexOf("/") + 1); // 传入一个远程图片的url,然后取最后的图片名字
File tmp = new File(Environment.getExternalStorageDirectory()+fileName);
if (!tmp.exists()) {
tmp.mkdir();
}
File f = new File(Environment.getExternalStorageDirectory()+fileName+"/"+bitmapURL);
f.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 保存图片到本地,这个是把图片压缩成字节流然后保存到本地,所以本地的图片是无法显示的
*
* @param mBitmap
*
* @param imageURL
*
* @param cxt
*/
public static void saveBitmap(Bitmap mBitmap, String imageURL, Context cxt) {
String bitmapName = imageURL.substring(imageURL.lastIndexOf("/") + 1); // 传入一个远程图片的url,然后取最后的图片名字
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = cxt.openFileOutput(bitmapName, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(byteArray);
} catch (Exception e) {
e.printStackTrace();
// 这里是保存文件产生异常
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// fos流关闭异常
e.printStackTrace();
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// oos流关闭异常
e.printStackTrace();
}
}
}
}
/**
* 读取本地私有文件夹的图片
*
* @param name
* @param cxt
* @return
*/
public static Bitmap getBitmap(String fileName, Context cxt) {
String bitmapName = fileName.substring(fileName.lastIndexOf("/") + 1);
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = cxt.openFileInput(bitmapName);
ois = new ObjectInputStream(fis);
byte[] byteArray = (byte[]) ois.readObject();
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
// 这里是读取文件产生异常
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// fis流关闭异常
e.printStackTrace();
}
}
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
// ois流关闭异常
e.printStackTrace();
}
}
}
// 读取产生异常,返回null
return null;
}
}
Android 图片在SD卡及包下的存储的更多相关文章
- BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...
- Android模拟器使用SD卡
在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一. 准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
此案例主要说的是Android使用GridView加载SD卡下所有目录,文件夹多层遍历,文件图标修改,设置文件对应打开方式等功能. 如图: 代码: public class GridViewFile ...
- Android 读取手机SD卡根目录下某个txt文件的文件内容
1.先看activity_main.xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- Android BaseAdapter ListView (SD卡中文件目录显示出来)
首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...
- Android开发之SD卡上文件操作
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- Android 虚拟机安装SD卡
在cmd命令行下,进入platform-tools目录下. 1.创建sdcard mksdcard -l mycard 256M E:\android\myCards\mysdcard.img ...
- Android_(控件)使用ListView显示Android系统中SD卡的文件列表
使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: 程序结构: <?xml ver ...
- android之读取SD卡状态
package xidian.dy.com.chujia; import android.os.Build; import android.os.Environment; import android ...
随机推荐
- Json 不同语言的使用
Javascript: 1.使用eval var parse_json_by_eval = function(str){ return eval('('+str+')'); } var value = ...
- 四、Chrome开发者工具详解(4)-Profiles面板
摘自: http://www.cnblogs.com/charliechu/p/6003713.html
- Notice:Array to string conversion的问题
如果后台或者前端输出这样的提示: Notice: Array to string conversion 原因是:用 echo 来输出数组,当然会报错,数组应该用print , print_r , 或 ...
- javascript 前段MVVM 框架
http://www.likebin.net/meteorlist.html http://www.cnblogs.com/sskyy/p/3197917.html
- Cube 数据 与 DW 数据对应不上
场景: 时间维度表:字段(日期) 收费事实表:字段(金额,收费日期,就诊编号) 管理:使用维度表的 日期字段与事实表的 收费日期字段 进行关联,建立多维度数据集. 问题: DW : 9月份 ...
- Invalid content was found starting with element 'mvc:exclude-mapping'.
问题?Invalid content was found starting with element 'mvc:exclude-mapping'. 这是springmvc中显著的错误,在配置拦截器的时 ...
- 使用git命令将本地项目上传到GIthub
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“.”,意为添加文 ...
- Linux which 查找命令
在学习 兄弟连 linux教学视频 的时候,我将所学的 linux 命令记录在我的博客中,方便自己查阅. 权限管理命令: which 基础的命令 命令名称:which 命令的所在路径:/usr/bin ...
- UGUI实现unity摇杆
http://www.winig.cc/archives/348 好久没有写文章了,最近在做项目是用的unity最新的ui系统UGUI,项目需要做一个摇杆,网上大部分都是用的插件和NGUI做的摇杆,u ...
- 快速对接payjs的个人支付接口(收银台模式)
近期在了解个人支付接口,希望能解决我在微信上支付的问题.找了很多平台对比再三,感觉payjs比较专业,其它多是模仿payjs的东西.同时支持支付宝和微信,由于本人支付宝还没开通(需要有一定流量才给开通 ...