Android camera2 api full screen image
In this episode of the android camera2 api we implement the android camera2 api full screen image to open once one of the recyclerview grid gallery thumbnails are clicked.
Get Code
The code is now on github you can get it from here
https://github.com/mobapptuts/recyclerview_image_gallery.git Tag camera2-single-image
or else run this command
git clone –branch camera2-single-image https://github.com/mobapptuts/recyclerview_image_gallery.git
Code Samples
CamaraIntentActivity.java
Create an Key for the intent
private static final String IMAGE_FILE_LOCATION = "image_file_location";
Create an intent for starting the single image activity
@Override public void getRecyclerViewAdapterPosition(int position) { // Toast.makeText(this, Integer.toString(position), Toast.LENGTH_SHORT).show(); Intent sendFileAddressIntent = new Intent(this, SingleImageActivity.class); sendFileAddressIntent.putExtra(IMAGE_FILE_LOCATION, sortFilesToLatest(mGalleryFolder)[position].toString()); startActivity(sendFileAddressIntent); }
Create the single image activity
SingleImageActivity.java
package nigelhenshaw.com.cameraintenttutorial; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.widget.ImageView; import android.widget.RelativeLayout; import java.io.File; /** * Created by nigelhenshaw on 5/10/2015. */ public class SingleImageActivity extends Activity { private static final String IMAGE_FILE_LOCATION = "image_file_location"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int width = displayMetrics.widthPixels; int height = displayMetrics.heightPixels; ImageView imageView = new ImageView(this); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( width, height ); imageView.setLayoutParams(params); setContentView(imageView); File imageFile = new File( getIntent().getStringExtra(IMAGE_FILE_LOCATION) ); SingleImageBitmapWorkerTask workerTask = new SingleImageBitmapWorkerTask(imageView, width, height); workerTask.execute(imageFile); } }
Create the worker task for loading the image off the main ui thread
SingleImageBitmapWorkerTask.java
package nigelhenshaw.com.cameraintenttutorial; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.widget.ImageView; import java.io.File; import java.lang.ref.WeakReference; /** * Created by nigelhenshaw on 6/07/2015. */ public class SingleImageBitmapWorkerTask extends AsyncTask<File, Void, Bitmap> { WeakReference<ImageView> imageViewReferences; final int TARGET_IMAGE_VIEW_WIDTH; final int TARGET_IMAGE_VIEW_HEIGHT; private File mImageFile; public SingleImageBitmapWorkerTask(ImageView imageView, int width, int height) { TARGET_IMAGE_VIEW_WIDTH = width; TARGET_IMAGE_VIEW_HEIGHT = height; imageViewReferences = new WeakReference<ImageView>(imageView); } @Override protected Bitmap doInBackground(File... params) { // return BitmapFactory.decodeFile(params[0].getAbsolutePath()); mImageFile = params[0]; // return decodeBitmapFromFile(params[0]); Bitmap bitmap = decodeBitmapFromFile(mImageFile); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { /* if(bitmap != null && imageViewReferences != null) { ImageView viewImage = imageViewReferences.get(); if(viewImage != null) { viewImage.setImageBitmap(bitmap); } } */ if(bitmap != null && imageViewReferences != null) { ImageView imageView = imageViewReferences.get(); if(imageView != null) { imageView.setImageBitmap(bitmap); } } } private int calculateInSampleSize(BitmapFactory.Options bmOptions) { final int photoWidth = bmOptions.outWidth; final int photoHeight = bmOptions.outHeight; int scaleFactor = 1; if(photoWidth > TARGET_IMAGE_VIEW_WIDTH || photoHeight > TARGET_IMAGE_VIEW_HEIGHT) { final int halfPhotoWidth = photoWidth/2; final int halfPhotoHeight = photoHeight/2; while(halfPhotoWidth/scaleFactor > TARGET_IMAGE_VIEW_WIDTH || halfPhotoHeight/scaleFactor > TARGET_IMAGE_VIEW_HEIGHT) { scaleFactor *= 2; } } return scaleFactor; } private Bitmap decodeBitmapFromFile(File imageFile) { BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(imageFile.getAbsolutePath(), bmOptions); bmOptions.inSampleSize = calculateInSampleSize(bmOptions); bmOptions.inJustDecodeBounds = false; return BitmapFactory.decodeFile(imageFile.getAbsolutePath(), bmOptions); } public File getImageFile() { return mImageFile; } }