Android拍照,剪下,並放入SD卡

來源:互聯網
上載者:User

標籤:android   http   io   ar   os   sp   java   for   on   

android拍照之後,先對圖片進行一次剪下,最後將圖片儲存到指定的目錄。在項目需要使用者拍照,並對圖片進行剪下後,發送到伺服器端做驗證。這裡貼出來一個小例子,能夠實現基本的功能。文章最後會給出來demo。介面上就是一個點擊事件。

1.首先點擊事件中啟動拍照,這裡寫死了圖片的名稱。實際上可以通過在公用的類中定義一個靜態變數來操作。

public void tackPhoto() {String status = Environment.getExternalStorageState();if (status.equals(Environment.MEDIA_MOUNTED)) {try {File filePath = Environment.getExternalStorageDirectory();Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);File f = new File(filePath, "test.jpg");Uri u = Uri.fromFile(f);intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);intent.putExtra(MediaStore.EXTRA_OUTPUT, u);startActivityForResult(intent, FLAG_CHOOSE_PHONE);} catch (ActivityNotFoundException e) {e.printStackTrace();}}}
2.在onActivityResult方法中獲得值
@Override       protected void onActivityResult (int requestCode, int resultCode, Intent data) {             if (resultCode == RESULT_OK) {                  File filePath = Environment.getExternalStorageDirectory();                  File f = new File(filePath, "test.jpg");                   if (requestCode == FLAG_CHOOSE_PHONE) {                        Toast. makeText( this, "FLAG_CHOOSE_PHONE", Toast.LENGTH_SHORT )                                    .show();                        String path = f.getAbsolutePath();                        Intent intent = new Intent();                        intent.setAction( "com.android.camera.action.CROP" );                        intent.setDataAndType(Uri. fromFile( newFile(path)), "image/*" );// mUri是已經選擇的圖片 Uri                        intent.putExtra( "crop", "true");                        intent.putExtra( "aspectX", 3); // 裁剪框比例                        intent.putExtra( "aspectY", 3);                        intent.putExtra( "outputX", 150); // 輸出圖片大小                        intent.putExtra( "outputY", 150);                        intent.putExtra( "return-data", true);                        startActivityForResult(intent,FLAG_CROP_PHONE);                  } else if (requestCode == FLAG_CROP_PHONE) {                        Bitmap bmap = data.getParcelableExtra( "data");                         if (bmap != null) {                               try {                                    bmap.compress(Bitmap.CompressFormat. JPEG, 0,                                                 newFileOutputStream(f));                              } catch (FileNotFoundException e) {                                    e.printStackTrace();                              }                        }                  }            }      }
3.最後要添加一些許可權
< uses-permission android:name = "android.permission.CAMERA" /><uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
: http://www.oschina.net/action/code/download?code=44069&id=61639

Android拍照,剪下,並放入SD卡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.