Android顯示相簿圖片和相機拍照

來源:互聯網
上載者:User

標籤:pen   oncreate   set   bsp   protected   dia   holo   asto   wrap   

首先看最重要的MainActive類:

 

 1 public class MainActivity extends AppCompatActivity { 2  3     private final int FROM_ALBUM = 1;//表示從相簿擷取照片 4     private final int FROM_CAMERA = 2;//表示從相機擷取照片 5     private ImageView imageView; 6  7     @Override 8     protected void onCreate(Bundle savedInstanceState) { 9         super.onCreate(savedInstanceState);10         setContentView(R.layout.activity_main);11     }12 13     // 開啟相簿14     public void onClickAlbum(View view){15         Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);16         startActivityForResult(intent, FROM_ALBUM);17     }18 19     // 開啟相機20     public void onClickCamera(View view){21         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);22         startActivityForResult(intent, FROM_CAMERA);23     }24 25     @Override26     protected void onActivityResult(int requestCode, int resultCode, Intent data){27 28         //從相簿返回29         if(requestCode == FROM_ALBUM  &&  resultCode == Activity.RESULT_OK  &&  data != null){30             imageView = (ImageView)findViewById(R.id.imageView);31             Uri imageUri = data.getData();32             ContentResolver cr = this.getContentResolver();33             try {34                 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri));40                 imageView.setImageBitmap(bitmap);41             }catch (FileNotFoundException e){42                 Log.e("Exception", e.getMessage(), e);43             }44         }45 46         //從相機返回47         if(requestCode == FROM_CAMERA  &&  resultCode == Activity.RESULT_OK  &&  data != null){48             imageView = (ImageView)findViewById(R.id.imageView);49             Bitmap photo = (Bitmap) data.getExtras().get("data");50 51             imageView.setImageBitmap(photo);52         }53 54         super.onActivityResult(requestCode, resultCode, data);55     }56 57 }

 

 

上面兩個按鈕的處理函數名稱在布局中定義,布局如下:兩個button(一個開啟相簿,一個開啟相機),一個imageview

 1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:app="http://schemas.android.com/apk/res-auto" 4     xmlns:tools="http://schemas.android.com/tools" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     tools:context="com.meitu.graydemo.MainActivity"> 8  9 <LinearLayout10     android:layout_width="368dp"11     android:layout_height="wrap_content"12     android:orientation="vertical"13     tools:layout_editor_absoluteY="0dp"14     tools:layout_editor_absoluteX="8dp">15 16     <LinearLayout17         android:id="@+id/buttonLayout"18         android:layout_width="fill_parent"19         android:layout_height="wrap_content"20         android:orientation="horizontal">21 22         <Button23             android:id="@+id/button"24             android:layout_width="wrap_content"25             android:layout_height="wrap_content"26             android:onClick="onClickAlbum"27             android:text="開啟相簿"28             tools:layout_editor_absoluteX="16dp"29             tools:layout_editor_absoluteY="16dp" />30 31         <Button32             android:id="@+id/button2"33             android:layout_width="wrap_content"34             android:layout_height="wrap_content"35             android:onClick="onClickCamera"36             android:text="開啟相機"37             tools:layout_editor_absoluteX="280dp"38             tools:layout_editor_absoluteY="16dp" />39     </LinearLayout>40 41     <ImageView42         android:id="@+id/imageView"43         android:layout_width="wrap_content"44         android:layout_height="wrap_content"45         app:srcCompat="@android:color/holo_blue_bright"46         tools:layout_editor_absoluteX="16dp"47         tools:layout_editor_absoluteY="48dp" />48 49 50 </LinearLayout>51 52 53 54 55 </android.support.constraint.ConstraintLayout>

 

Android顯示相簿圖片和相機拍照

相關文章

聯繫我們

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