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 ...
随机推荐
- bzoj 2179 FFT快速傅立叶 —— FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2179 默写板子,注释的是忘记的地方. 代码如下: #include<iostream& ...
- rsync(三)算法原理和工作流程分析
在开始分析算法原理之前,简单说明下rsync的增量传输功能. 假设待传输文件为A,如果目标路径下没有文件A,则rsync会直接传输文件A,如果目标路径下已存在文件A,则发送端视情况决定是否要传输文件A ...
- PTA-数据结构 Dijkstra 城市间紧急救援
城市间紧急救援(25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标 ...
- mfc给对话框添加背景
void CMyProjetDlg::OnPaint() { CPaintDC dc(this);//用于绘制设备的上下文 CRect rect; GetClientRect(&rect); ...
- 重载和const形参的学习心得
在C++ primer 5th中,第6章的练习6.39(a)中,关于重载和const形参的学习心得. 练习6.39:说明在下面的每组声明中第二条声明语句是何含义.如果有非法的声明,请指出来. (a) ...
- 爬取动态网页:Selenium
参考:http://blog.csdn.net/wgyscsf/article/details/53454910 概述 在爬虫过程中,一般情况下都是直接解析html源码进行分析解析即可.但是,有一种情 ...
- 【Data Structure & Algorithm】字符串全排列
字符串全排列 题目:输入一个字符串,打印出该字符串的所有排列.例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab.cba. 分析:考察对递归的理 ...
- Flutter实战视频-移动电商-51.购物车_Provide中添加商品
51.购物车_Provide中添加商品 新加provide的cart.dart页面 引入三个文件.开始写provide类.provide需要用with 进行混入 从prefs里面获取到数据,判断有没有 ...
- UVa 10891 Game of Sum (DP)
题意:给定一个长度为n的整数序列,两个人轮流从左端或者右端拿数,A先取,问最后A的得分-B的得分的结果. 析:dp[i][j] 表示序列 i~j 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...
- LeetCode: 620 Not Boring Movies(easy)
题目: X city opened a new cinema, many people would like to go to this cinema. The cinema also gives o ...