This blog post contains supplementary material for a video from my YouTube channel. In this video, I will be covering how to access and save photos in an Android application.

You can fork or download the starter code on Github.

There are 15 steps in total.

Step 1. Define a new constant.

private static final int GALLERY_REQUEST_CODE = 100;

view raw

step1.java

hosted with ❤ by GitHub

Step 2. Open the gallery.

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_REQUEST_CODE);

view raw

step2.java

hosted with ❤ by GitHub

Step 3. Handle the Activity results.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == GALLERY_REQUEST_CODE) {
try {
Uri selectedImage = data.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
selectedImageView.setImageBitmap(BitmapFactory.decodeStream(imageStream));
} catch (IOException exception) {
exception.printStackTrace();
}
}
}

view raw

step3.java

hosted with ❤ by GitHub

Step 4. Add a camera request code.

private static final int CAMERA_REQUEST_CODE = 200;

view raw

step4.java

hosted with ❤ by GitHub

Step 5. Launch a camera activity.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}

view raw

step5.java

hosted with ❤ by GitHub

Step 6. Handle the camera response.

if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
selectedImageView.setImageBitmap(imageBitmap);
}

view raw

step6.java

hosted with ❤ by GitHub

Step 7. Add the image variable.

private Bitmap image;

view raw

step7.java

hosted with ❤ by GitHub

Step 8. Accept a bitmap variable as a parameter.

public Memory(String title, Bitmap image) {
this.title = title;
this.image = image;
}

view raw

step8.java

hosted with ❤ by GitHub

Step 9. Add a new column.

private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + MemoryContract.MemoryEntry.TABLE_NAME + " (" +
MemoryContract.MemoryEntry._ID + INTEGER_TYPE + " PRIMARY KEY" + COMMA_SEP +
MemoryContract.MemoryEntry.COLUMN_IMAGE + TEXT_TYPE + COMMA_SEP +
MemoryContract.MemoryEntry.COLUMN_TITLE + TEXT_TYPE + " )";

view raw

step9.java

hosted with ❤ by GitHub

Step 10. Add a new pair.

values.put(MemoryContract.MemoryEntry.COLUMN_IMAGE, memory.getImageAsString());

view raw

step10.java

hosted with ❤ by GitHub

Step 11. Add a new getter.

public String getImageAsString() {
return bitmapToString(this.image);
}

view raw

step11.java

hosted with ❤ by GitHub

Step 12. Convert an image into a string and back.

private static String bitmapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
private static Bitmap stringToBitmap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
} catch (Exception e) {
e.getMessage();
return null;
}
}

view raw

step12.java

hosted with ❤ by GitHub

Step 13. Read the new column.

this.image = cursor.getString(COL_IMAGE);

view raw

step13.java

hosted with ❤ by GitHub

Step 14. Display the drawable from the database.

holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.placeholder));

view raw

step14.java

hosted with ❤ by GitHub

Step 15. Resize the image before storing it in the database.

private static final float PREFERRED_WIDTH = 250;
private static final float PREFERRED_HEIGHT = 250;
public static Bitmap resizeBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = PREFERRED_WIDTH / width;
float scaleHeight = PREFERRED_HEIGHT / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(
bitmap, 0, 0, width, height, matrix, false);
bitmap.recycle();
return resizedBitmap;
}

view raw

step15.java

hosted with ❤ by GitHub

You can find the complete code in this repository.

Hope you found this tutorial useful! Let me know if you have any questions. 🙂

%d bloggers like this: