Android之Bitmap使用心得(持續更新)

來源:互聯網
上載者:User

因為此代碼裡面有解釋,因此直接上代碼:

public class ChangeBitmapPixel extends Activity {
    private Button btn;
    private Bitmap photo;
    private ImageView image;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    private ByteArrayOutputStream baos;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        image = (ImageView) findViewById(R.id.image);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
                //如果使用下面注釋的代碼,將不返回資料給Intent
//                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
//                        .getExternalStorageDirectory(),"pic.jpg")));
                //啟動網路攝影機並且在拍攝後返回
                startActivityForResult(i, 10);
            }});
        
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //兩種讀取檔案的方法
         Uri uri = data.getData();
         if (uri != null) {
             System.out.println("uri不為空白");
             photo = BitmapFactory.decodeFile(uri.getPath());
             System.out.println("uri:"+photo);
         }
         
          if (photo == null) {
              Bundle bundle = data.getExtras();
              if (bundle != null) {
                  photo = (Bitmap) bundle.get("data");
                  image.setImageBitmap(photo);
                  //儲存照片
                  savePic(photo);
                  System.out.println("photo:"+photo);
              } else {
                  Toast.makeText(ChangeBitmapPixel.this,
                          "為空白",
                          Toast.LENGTH_LONG).show();
                  return;
              }
          }
        super.onActivityResult(requestCode, resultCode, data);
        }
    public void savePic(Bitmap bitmap){
        //使用此流讀取
        baos = new ByteArrayOutputStream();
        //第二個參數影響的是圖片的品質,但是圖片的寬度與高度是不會受影響滴
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        //這個函數能夠設定圖片的寬度與高度
        //Bitmap map = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
        //把資料轉為為位元組數組
        byte[] byteArray = baos.toByteArray();
        try {
            fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"pic.jpg");
            bos = new BufferedOutputStream(fos);
            bos.write(byteArray);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                baos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.