圖片文件和Bitmap之間的轉換


圖片文件轉為Bitmap對象
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();
            }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM