0

Problem: The user may take/select up to 3 photos. I'm having trouble in figuring out how to fill the 3 cases; I'm not sure how I could do to retrieve the corresponding ImageView ID.

Here is the UI

I tried the putextra since I'm using Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE), but it seems that it's not possible to use the putextra method (I don't retrieve any extra)

So let me share the code with you and feel free to let me know if you would proceed differently. Thanks a lot!

So here I'm catching the click event and passing the V.getID to the method that will handle the actions related to selecting/taking photos.

@Override
public void onClick(View v) {

    switch(v.getId()){
        case R.id.add_item_give_button:
            checkAddedItem();
            break;
        case R.id.add_item_image_1:
            selectImage(v.getId());
            break;
        case R.id.add_item_image_2:
            selectImage(v.getId());
            break;
        case R.id.add_item_image_3:
            selectImage(v.getId());
            break;
    }
}

The selectImage method is called and will handle the alertDialog that will ask if the user wants either to take a picture or to select one. I'm trying to pass the ID in the putExtra method, but nothing is received in the startActivityForResult

public void selectImage(final int imageViewID){

    final CharSequence[] options = {getString(R.string.cameral_select_photo_label), getString(R.string.camera_take_photo_label), getString(R.string.common_cancel_label)};
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getString(R.string.camera_dialog_title_label));

    builder.setItems(options, new DialogInterface.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(options[which].equals(getString(R.string.camera_take_photo_label))){

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra("ImageViewID", imageViewID);
                startActivityForResult(intent, REQUEST_CAMERA);

            }
            else if(options[which].equals(getString(R.string.cameral_select_photo_label))){

                Utils.verifyStoragePermissions(getActivity());

                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                );

                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, getResources().getText(R.string.camera_select_image)),SELECT_FILE);

            }
            else if(options[which].equals(getString(R.string.common_cancel_label))){

                dialog.dismiss();

            }
        }
    });
    builder.show();
}

In the startActivityForResult, I don't receive the ImageViewID. So for now, I'm just putting the image in the first ImageView since I'm not able to retrieve the right ID.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_OK){

        if(requestCode == REQUEST_CAMERA){

            Log.d("Data content", String.valueOf(data));

            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

            File destination = new File(Environment.getExternalStorageDirectory(),
                    System.currentTimeMillis() + ".jpg");

            FileOutputStream fo;

            try {
                destination.createNewFile();
                fo = new FileOutputStream(destination);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            itemPic1.setImageBitmap(thumbnail);

        } else if (requestCode == SELECT_FILE){

            Log.d("imageViewOrigin", String.valueOf(data.getIntExtra("imageViewID", 0)));

            Uri selectedImageUrl = data.getData();
            String[] projection = {MediaStore.MediaColumns.DATA};
            CursorLoader cursorLoader = new CursorLoader(getContext(), selectedImageUrl, projection, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();

            String selectedImagePath = cursor.getString(column_index);

            Bitmap bm;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(selectedImagePath, options);
            final int REQUIRED_SIZE = 200;
            int scale = 1;
            while(options.outWidth / scale / 2 >= REQUIRED_SIZE && options.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale += 2;
            options.inSampleSize = scale;
            options.inJustDecodeBounds = false;
            bm = BitmapFactory.decodeFile(selectedImagePath, options);

            itemPic1.setImageBitmap(bm);


        }

    }

}
Isabelle
  • 1,457
  • 5
  • 19
  • 44

2 Answers2

0

I would recommend setting tags on the ImageViews. Follow the link, its a similar issue What is the main purpose of setTag() getTag() methods of View?. Let me know if you need more help!

Community
  • 1
  • 1
fsebek
  • 389
  • 2
  • 9
  • Thank you Feddy, but I'm afraid I would face the same issue. Thanks anyway! – Isabelle Apr 20 '16 at 15:22
  • Sorry, I think I understand now. Instead on settings the click listener like that create separate TextView objects as public class objects. For example TextView firstTextView = (TextView) findViewById(R.id."id here"); From there you can add individual clicks listeners to the objects once they are initalized. Simply put firstTextView.setOnClick and then Android Studio will automatically fill the rest for you. (Read comment below) – fsebek Apr 20 '16 at 20:15
  • Then within that you can call that method and also pass the textview with that method. public void selectImage(final int imageViewID, TextView clickedTextView). Then in the method you can simply just set the bitmap to the passed textview. Let me know if you need more help! Sorry for the late reply. Made it 2 comments since it was way to long. – fsebek Apr 20 '16 at 20:18
0

Try this way :

 private void openImageIntent(int IMAGE_TYPE) {

    // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "mycapturedImage" + File.separator);
    root.mkdirs();
    final String fname = getUniqueImageFilename();
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);

    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Choisir une Source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    startActivityForResult(chooserIntent, IMAGE_TYPE);
}

Then retrieve each Image like this :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_CANCELED) {
        if (resultCode == RESULT_OK) {
            if (requestCode == FIRST_IMAGE_INTENT) {
                final boolean isCamera;
                if (data == null) {
                    isCamera = true;

                } else {
                    final String action = data.getAction();
                    if (action == null) {
                        isCamera = false;
                    } else {
                        isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                }


                if (isCamera) {

                    selectedCaptureUri = outputFileUri; 

                } else {

                    selectedCaptureUri = data == null ? null : data.getData();


                }

   //Display image here 

            } else if (requestCode == SECOND_PICTURE_INTENT) {...}
bashizip
  • 562
  • 5
  • 14