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; |
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); |
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(); | |
} | |
} | |
} |
Step 4. Add a camera request code.
private static final int CAMERA_REQUEST_CODE = 200; |
Step 5. Launch a camera activity.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { | |
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE); | |
} |
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); | |
} |
Step 7. Add the image variable.
private Bitmap image; |
Step 8. Accept a bitmap variable as a parameter.
public Memory(String title, Bitmap image) { | |
this.title = title; | |
this.image = image; | |
} |
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 + " )"; |
Step 10. Add a new pair.
values.put(MemoryContract.MemoryEntry.COLUMN_IMAGE, memory.getImageAsString()); |
Step 11. Add a new getter.
public String getImageAsString() { | |
return bitmapToString(this.image); | |
} |
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; | |
} | |
} |
Step 13. Read the new column.
this.image = cursor.getString(COL_IMAGE); |
Step 14. Display the drawable from the database.
holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.placeholder)); |
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; | |
} |
You can find the complete code in this repository.
Hope you found this tutorial useful! Let me know if you have any questions. 🙂
This is just excellent!!!
Vlad, this is so helpful and a great resource for people like me that want to learn more and more 🙂 These helped me to understand better everything 🙂
Thank you so much for this amazing article!
Good video. Ditch the music, it is very distraction g. This is an educational video, not entertainment. My opinion.
Thank you for sharing Sean. I’ll make sure to listen to the feedback for the next video.
when i click image it get blur pls help me
is there a way to handle onClickListener to images?