An image is a UI widget used to show images or upload images in an Android app. It is the sub-class of the View class. We can use it to add many images, like Bitmaps, and Drawable files. To use it a person can add ImageView to their XML files. Passing its id to the resource file. A sample video is given below to get an idea about what we are going to do in this article.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:orientation = "vertical" android:gravity = "center_horizontal" android:layout_height = "match_parent" tools:context = ".MainActivity" > < ImageView android:layout_width = "300dp" android:layout_marginTop = "100dp" android:layout_height = "400dp" android:scaleType = "fitXY" android:id = "@+id/imgCamera" /> < Button android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/btnCamera" android:layout_marginTop = "21dp" android:backgroundTint = "#328736" android:text = "Open Camera" /> < Button android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/btnGallery" android:backgroundTint = "#328736" android:layout_marginTop = "21dp" android:text = "Open Gallery" /> </ LinearLayout > |
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
package com.shruti.cameraexample; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private final int CAMERA_REQ_CODE = 100 ; private final int GALLERY_REQ_CODE = 1000 ; ImageView imgCamera; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgCamera=findViewById(R.id.imgCamera); Button btnCamera = findViewById(R.id.btnCamera); Button btnGallery = findViewById(R.id.btnGallery); btnGallery.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent iGallery = new Intent(Intent.ACTION_PICK); iGallery.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(iGallery,GALLERY_REQ_CODE); } }); btnCamera.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(iCamera,CAMERA_REQ_CODE); } }); } @Override protected void onActivityResult( int requestCode, int resultCode, @Nullable Intent data) { super .onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK){ if (requestCode==GALLERY_REQ_CODE){ // gallery imgCamera.setImageURI(data.getData()); } else if (requestCode==CAMERA_REQ_CODE) { Bitmap img = (Bitmap)(data.getExtras().get( "data" )); imgCamera.setImageBitmap(img); } } imgCamera.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { BitmapDrawable bitmapDrawable = (BitmapDrawable) imgCamera.getDrawable(); Bitmap bitmap = bitmapDrawable.getBitmap(); String bitmpath = MediaStore.Images.Media.insertImage(getContentResolver(),bitmap, "upload" , null ); Uri uri = Uri.parse(bitmpath); Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType( "image/png" ); shareIntent.setPackage( "com.whatsapp" ); shareIntent.putExtra(Intent.EXTRA_STREAM,uri); startActivity(Intent.createChooser(shareIntent, "Share Using" )); } }); } } |
Step 4:
In your Manifest file add these permissions.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Output:
Note: In your app setting allow all the permissions to access your external storage. If you will not do so the app will not work properly.