Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active January 11, 2017 06:13
Show Gist options
  • Save pokk/b031bb81d95fd4145cd8f220e32d1718 to your computer and use it in GitHub Desktop.
Save pokk/b031bb81d95fd4145cd8f220e32d1718 to your computer and use it in GitHub Desktop.
How to pick photos from build-in photos.

Introduction

We can choose a photo from many source places. For example: photos, camera, file manager, etc.

Pick photos from build-in photo app.

  1. Just pick one photo.
  2. Pick multiple photos once time.

Pick a photo from build-in camera app.

Get a photo from camera.

Pick a file or a photo from phone.

public static final CAMERA_REQUEST_CODE = 5566;
public static Uri imageUri = null;
// Trigger a build-in camera application.
public onPickPhotoThruCamera()
{
// Create intent for taking a picture and choose it.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Set a file path for putting the picture temporally.
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
if (file.exists())
{
file.delete();
file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
}
imageUri = Uri.fromFile(file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// If you call startActivityForResult() using an intent that no app can handle, your app will crash.
// So as long as the result is not null, it's safe to use the intent.
if (intent.resolveActivity(getPackageManager()) != null)
{
// Bring up gallery to select a photo
startActivityForResult(Intent.createChooser(intent, "Take a picture"), CAMERA_REQUEST_CODE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Uri selectedImageUri = imageUri;
getContentResolver().notifyChange(selectedImageUri, null);
try
{
this.getFragmentObject(R.id.fragmentContainer, TantraFragment.class).sendPhotoToServer(selectedImageUri.toString());
Toast.makeText(this, selectedImageUri.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
<!- ... -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!- ... -->
</manifest>
// PICK_PHOTO_CODE is a constant integer
public final static int PICK_PHOTO_CODE = 1046;
// Trigger gallery selection for a photo
public void onPickPhoto(View view)
{
// Create intent for picking a photo from the gallery
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// If you call startActivityForResult() using an intent that no app can handle, your app will crash.
// So as long as the result is not null, it's safe to use the intent.
if (intent.resolveActivity(getPackageManager()) != null)
{
// Bring up gallery to select a photo
startActivityForResult(intent, PICK_PHOTO_CODE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (data != null)
{
Uri photoUri = data.getData();
// Do something with the photo based on Uri
Bitmap selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
// Load the selected image into a preview
ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview);
ivPreview.setImageBitmap(selectedImage);
}
}
// PICK_PHOTO_CODE is a constant integer
public final static int PICK_PHOTO_CODE = 1046;
// Trigger gallery selection for a photo
public void onPickPhoto(View view)
{
// Create intent for picking a photo from the gallery
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
// If you call startActivityForResult() using an intent that no app can handle, your app will crash.
// So as long as the result is not null, it's safe to use the intent.
if (intent.resolveActivity(getPackageManager()) != null)
{
// Bring up gallery to select a photo
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_PHOTO_CODE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (PICK_PHOTO_CODE == resultCode && null != data)
{
if (null != data.getClipData())
ClipData mClipData = data.getClipData();
mArrayUri = new ArrayList<Uri>();
mBitmapsSelected = new ArrayList<Bitmap>();
for (int i = 0; i < mClipData.getItemCount(); i++)
{
ClipData.Item item = mClipData.getItemAt(i);
mArrayUri.add(item.getUri());
// !! You may need to resize the image if it's too large
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
mBitmapsSelected.add(bitmap);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment