Android studio - Open the gallery, select images, capture a photo with camera

Introduction
This article explains how to open the gallery in your phone and display the selected images, capture a photo from the camera and save it to the gallery of your Android phone.

First the user will need to choose if he/she wants to select an image from the gallery or wants to capture an image from the camera. Then depending on the option chosen by the user,  we will either open the gallery or capture an image. 
Step 1:
Open "AndroidManifest.xml" and add the following code to it: 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chhavi.uploadingandviewimage"
    android:versionCode="1"
    android:versionName="1.0" >

  <uses-permission android:name="android.permission.CAMERA" />
  <uses-feature android:name="android.hardware.camera" />
  <uses-feature android:name="android.hardware.camera.autofocus" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  <uses-sdk
      android:minSdkVersion="7"
      android:targetSdkVersion="16" />

  <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
    <activity
        android:name="com.chhavi.uploadingandviewimage.MainActivity"
        android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
</manifest> 


Note the permission "WRITE_EXTERNAL_STORAGE" is to save the image captured to the gallery. "CAMERA" permissions enables use of the camera of your Android phone.
Step 2:
Open "activity_main" and add the following code to it:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/LinearLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="10dp" >

  <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:padding="5dp" >

    <Button
            android:id="@+id/btnSelectPhoto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Photo" />

  </LinearLayout>

  <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="10dp" >

    <ImageView
            android:id="@+id/viewImage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/camera" />

  </LinearLayout>


</LinearLayout> 


Aucun commentaire:

Enregistrer un commentaire