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 ...
随机推荐
- linux drwxr-xr-x 什么意思
第一位表示文件类型. d:是目录文件, l:是链接文件, -:是普通文件, p:是管道 第2-4位表示这个文件的属主拥有的权限,r是读,w是写,x是执行.(其中r是4,w是2,x是1) 第5-7位表示 ...
- IOS Carthage安装、使用
一.Carthage的安装和使用1.安装homebrew后输入如下命令 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercont ...
- Pro Android学习笔记(一五四):传感器(4):陀螺仪、加速传感器
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei. 陀螺仪 陀螺仪(Gyr ...
- [HNOI2010] 弹飞绵羊 bounce
标签:分块.题解: 200000,而且标号从0开始,很符合分块的条件啊.看看怎么实现. 首先分成√n个区间,然后如果我们对于每一个位置i,求出一个Next[i]和step[i],分别表示跳到的后一个位 ...
- python 之 函数 迭代器
5.9 迭代器 5.91 可迭代对象和迭代器对象 1.什么是迭代?:迭代是一个重复的过程,并且每次重复都是基于上一次的结果而来 2.要想了解迭代器到底是什么?必须先了解一个概念,即什么是可迭代的对象? ...
- Phpstorm建立连接Wampserver的数据库
phpstorm是一款php集成开发环境软件,集成了很多功能,不但有强大的代码编辑及调试功能,还能连接数据库.本文写的就是如何用phpstorm来建立访问wampserver数据库,查询输出数据,方便 ...
- win10子系统linux编译ffmpeg
android-ndk-r14b(linux版) ffmpeg-4.0 开启win10子系统(控制面板->程序和功能->启用或关闭Windows功能 然后在 适用与 Linux 的 Win ...
- [题解](tarjan割点/点双)luogu_P3225_矿场搭建
首先和割点有关,求割点,然后这些割点应该把这个图分成了多个点双,可以考虑点双的缩点,假如缩点做的话我们要分析每个点双的性质和贡献 先拿出一个点双来,如果它没有连接着割点,那么至少要建两个,以防止其中一 ...
- 洛谷 P3462 [POI2007]ODW-Weights
题面: https://www.luogu.org/problemnew/show/P3462 https://www.lydsy.com/JudgeOnline/problem.php?id=111 ...
- 洛谷 P1463 [HAOI2007]反素数
https://www.luogu.org/problemnew/show/P1463 注意到答案就是要求1-n中约数最多的那个数(约数个数相同的取较小的) 根据约数个数的公式,在约数个数相同的情况下 ...