Android 图片在SD卡及包下的存储
2015-07-01 16:57
459 查看
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卡中并从SDk卡中读取(通过Bitmap方法)
- Android SD卡存储图片
- 我的Android之旅(十八)---数据存储--向SD卡保存网络上的图片
- Android 存储网络图片到SD卡,并且无网络状态时显示圆形头像
- Android 图片在SD卡及包下的存储
- android调用相机并将照片存储到sd卡上
- android 7.0以上裁剪和选择sd卡外面的图片适配(日记)
- Android存储(读取)之SD卡
- Android监听外部存储设备的状态(SD卡、U盘等等)
- Android——FileOutputStream与openFileOutput()的区别分析【第一个用于文件存储,第二个用于SD卡存储】【转】
- Android实现下载图片并保存到SD卡中
- Android中图片URI如何转换成对应的存储路径
- Android 存储学习之保存系统短信到SD卡(使用XML序列化器)
- Android 图像存储在SD卡ContentResolver
- 21、从头学Android之Android的数据存储--SD卡
- Android使用sqlliteOpenhelper更改数据库的存储路径放到SD卡上
- android sd卡读取图片 并处理大图
- Android开发九:从网上下载文件并存储到SD卡中
- 【转】android:网络图片转为bitmap 保存至SD卡中
- Android数据库SD卡创建,及图片存、取操作!!