1. 程式人生 > >Android 圖片文件和Bitmap之間的轉換

Android 圖片文件和Bitmap之間的轉換

對象 如果 path str return stack tac mapfile try

String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath);

如果圖片過大,可能導致Bitmap對象裝不下圖片
解決辦法:

String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath,getBitmapOption(2)); //將圖片的長和寬縮小味原來的1/2

private Options getBitmapOption(int inSampleSize){
        System.gc();
        BitmapFactory.Options options 
= new BitmapFactory.Options(); options.inPurgeable = true; options.inSampleSize = inSampleSize; return options; }

Bitmap對象保存味圖片文件

public void saveBitmapFile(Bitmap bitmap){
            File file=new File("/mnt/sdcard/pic/01.jpg");//將要保存圖片的路徑
            try {
                    BufferedOutputStream bos 
= new BufferedOutputStream(new FileOutputStream(file)); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); } catch (IOException e) { e.printStackTrace(); } }

Android 圖片文件和Bitmap之間的轉換