How to make the user to upload photos in imageView from the gallery?

  • where does the user want to download them from? - Vladyslav Matviienko
  • From the phone, from your gallery - Ker_ Jen
  • one
    Then you actually need 2 questions: 1. How to upload an image to the ImageView from the gallery. 2. How to make a selection of images from the gallery. Set them separately. - Vladyslav Matviienko

1 answer 1

First you need to activate the gallery system, you can do it like this:

/** * Method for call native activity for pick images */ private void callPickedImageActivity() { Intent intent = new Intent(); // Show only images, no videos or anything else intent.setType("image/*"); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); intent.setAction(Intent.ACTION_GET_CONTENT); // Always show the chooser (if there are multiple options available) startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_MULTIPLE_REQUEST_CODE); } 

In onActivityResult you need to get a result like this:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case PICK_IMAGE_MULTIPLE_REQUEST_CODE: extractPickedImages(requestCode, resultCode, data); break; } } /** * Method for extract result picked images * * @param requestCode * @param resultCode * @param data */ private void extractPickedImages(int requestCode, int resultCode, Intent data) { String realPath; try { // When an Image is picked if (requestCode == PICK_IMAGE_MULTIPLE_REQUEST_CODE && resultCode == Activity.RESULT_OK && null != data) { imagesPathList = new ArrayList<String>(); if (data.getData() != null) { if (Build.VERSION.SDK_INT < 11) realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData()); // SDK >= 11 && SDK < 19 else if (Build.VERSION.SDK_INT < 19) realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData()); // SDK > 19 (Android 4.4) else realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData()); Log.e(TAG, "imageEncoded:" + realPath); } else { if (data.getClipData() != null) { ClipData mClipData = data.getClipData(); ArrayList<Uri> mArrayUri = new ArrayList<Uri>(); for (int i = 0; i < mClipData.getItemCount(); i++) { ClipData.Item item = mClipData.getItemAt(i); Uri uri = item.getUri(); if (Build.VERSION.SDK_INT < 11) realPath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, uri); // SDK >= 11 && SDK < 19 else if (Build.VERSION.SDK_INT < 19) realPath = RealPathUtil.getRealPathFromURI_API11to18(this, uri); // SDK > 19 (Android 4.4) else realPath = RealPathUtil.getRealPathFromURI_API19(this, uri); Log.e(TAG, "imageEncoded:" + realPath); imagesPathList.add(realPath); } Log.v("LOG_TAG", "Selected Images" + mArrayUri.size()); } } } else { Toast.makeText(getApplicationContext(), "You haven't picked Image", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG) .show(); } } 

RealPathUtil.java

 public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second chat_view_item in the array String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } 

Well, install in ImageView, you can:

 Drawable drawable = Drawable.createFromPath(path); mImageView.setImageDrawable(drawable);