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;
    }

}
相關文章
相關標籤/搜索