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卡及包下的存储的更多相关文章

  1. BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...

  2. Android模拟器使用SD卡

    在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一.      准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...

  3. Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等

    此案例主要说的是Android使用GridView加载SD卡下所有目录,文件夹多层遍历,文件图标修改,设置文件对应打开方式等功能. 如图: 代码: public class GridViewFile ...

  4. Android 读取手机SD卡根目录下某个txt文件的文件内容

    1.先看activity_main.xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  5. Android BaseAdapter ListView (SD卡中文件目录显示出来)

    首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...

  6. Android开发之SD卡上文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  7. Android 虚拟机安装SD卡

    在cmd命令行下,进入platform-tools目录下.   1.创建sdcard   mksdcard -l mycard 256M E:\android\myCards\mysdcard.img ...

  8. Android_(控件)使用ListView显示Android系统中SD卡的文件列表

    使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: 程序结构: <?xml ver ...

  9. android之读取SD卡状态

    package xidian.dy.com.chujia; import android.os.Build; import android.os.Environment; import android ...

随机推荐

  1. openstack封装镜像

    1.准备工作:准备你想要封装的各种镜像的iso,完整版本最简单版本都ok,只要能出虚拟机就行,这个大家去官网下载自己要的iso就可以,我这里用centos6.4最简版本,因为分给我的活让做这个的... ...

  2. socket学习目录

    深入探析c# Socket http://www.cnblogs.com/tianzhiliang/archive/2010/09/08/1821623.html Http和Socket连接区别 ht ...

  3. Hadoop 源代码组织结构

    Hadoop 2.X 包括 编译好的可以直接部署的文件hadoop-{VERSION}.tar.gz; 还有源代码文件hadoop-{VERSION}-src.tar.gz , 需要 Maven 编译 ...

  4. LuaToC#

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. aria2安装webui

    安装aria2 yum install aria2 安装完成后可以使用简单命令进行下载 aria2c http://example.org/mylinux.iso aria2c -c -s http: ...

  6. IntelliJ IDEA的自动提示貌似是区分大小写的,首字母小写的话,怎么都提示不出来。

    IntelliJ IDEA的自动提示貌似是区分大小写的,首字母小写的话,怎么都提示不出来. File>Settings>editor >general >code comple ...

  7. char*和CStringA互转

    char*->CStringA char* ch1 = "中文测试123"; CStringA str(ch1); CStringA->char* char* ch2 ...

  8. css 3d旋转

  9. [HNOI2010] 矩阵 matrix

    标签:dfs+剪枝. 题解: 这道题看着就像一道dfs题目,没有什么算法可以用来算这个东西,于是想想暴搜. 如果我们确定因为是2*2的子矩阵的和,如果确定了其中三个,那么就可以确定第四个,发现如果确定 ...

  10. AVAudioPlayer 如何在页面呈现之后按需初始化

    在页面中按需初始化 AVAudioPlayer 很多时候我们需要根据页面上内容的情况创建 AVAudioPlayer 对象,已达到降低无谓资源占用等目的.下面我们来看一段代码看起来正确的代码: ove ...