Borom1r
Borom1r

Reputation: 155

set multiple photo in imageview android

no iam building some picture gallery (attachment) where user can upload 5 photos. so i want to set multiple photo in imageview using ActivityResult and to do so you have to upload it from every imageview

here is my code

 ivAttachment1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
        }
    });
    ivAttachment2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
        }
    });
    ivAttachment3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
        }
    });
    ivAttachment4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
        }
    });
    ivAttachment5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
        }
    });
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && data != null) {
        //get picture path
        selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);

        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);

        Bitmap bm = BitmapFactory.decodeFile(picturePath);

        int width = bm.getWidth();
        int height = bm.getHeight();

        //convert to base64
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
        byte[] b = baos.toByteArray();
        strPhoto1 = Base64.encodeToString(b, Base64.NO_WRAP);
        strPhoto2 = Base64.encodeToString(b, Base64.NO_WRAP);
        strPhoto3 = Base64.encodeToString(b, Base64.NO_WRAP);
        strPhoto4 = Base64.encodeToString(b, Base64.NO_WRAP);
        strPhoto5 = Base64.encodeToString(b, Base64.NO_WRAP);
        //show the image
        ivAttachment1.setImageBitmap(bm);
        ivAttachment2.setImageBitmap(bm);
        ivAttachment3.setImageBitmap(bm);
        ivAttachment4.setImageBitmap(bm);
        ivAttachment5.setImageBitmap(bm);

    }
}

and here is my layout code

<RelativeLayout
            android:layout_width="match_parent"
            android:id="@+id/rlAttachment"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_marginLeft="15px"
                android:id="@+id/ivAttachment1"
                android:src="@drawable/profpic" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_margin="15px"
                android:src="@drawable/profpic"
                android:id="@+id/ivAttachment2"
                android:layout_below="@id/ivAttachment1"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_margin="15px"
                android:id="@+id/ivAttachment3"
                android:layout_below="@id/ivAttachment2"
                android:src="@drawable/profpic" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_margin="15px"
                android:layout_below="@id/ivAttachment3"
                android:id="@+id/ivAttachment4"
                android:src="@drawable/profpic" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_below="@id/ivAttachment4"
                android:id="@+id/ivAttachment5"
                android:layout_margin="15px"
                android:src="@drawable/profpic" />
        </RelativeLayout>

but the problem is when i upload in ivAttachment1, the others will uploaded in the same picture in ivAttachment2,3,4 and 5. i need to create some kind of "switch" method perhaps. and also i have to put every bitmap photo to string so i can save it to my database. can you show me the fatest way ?

thank you

Upvotes: 0

Views: 105

Answers (1)

Rajendra
Rajendra

Reputation: 504

You will have to keep which ImageView is clicked and then in onActivityResult check for the clicked ImageView and then do the further action.

e.g. create an enum or integer variable in your class:

 private int CLICKED_IMAGEVIEW=0;  

        ivAttachment1.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View v) {
        CLICKED_IMAGEVIEW=1;
                    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
                    startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE); 
                } 
            }); 
            ivAttachment2.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View v) { 
        CLICKED_IMAGEVIEW=2;
                    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
                    startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE); 
                } 
            }); 
            ivAttachment3.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View v) { 
        CLICKED_IMAGEVIEW=3;
                    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
                    startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE); 
                } 
            }); 
            ivAttachment4.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View v) { 
        CLICKED_IMAGEVIEW=4;
                    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
                    startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE); 
                } 
            }); 
            ivAttachment5.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View v) { 
        CLICKED_IMAGEVIEW=5;
                    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
                    startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE); 
                } 
            }); 
         @Override 
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && data != null) {
                //get picture path 
                selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);

                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);

                Bitmap bm = BitmapFactory.decodeFile(picturePath);

                int width = bm.getWidth();
                int height = bm.getHeight();

                //convert to base64 
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
                byte[] b = baos.toByteArray();
    switch(CLICKED_IMAGEVIEW)
        {
    case 1:  strPhoto1 = Base64.encodeToString(b, Base64.NO_WRAP);
        //show the image 
                ivAttachment1.setImageBitmap(bm);
           break;
    case 2:  strPhoto2 = Base64.encodeToString(b, Base64.NO_WRAP);
        ivAttachment2.setImageBitmap(bm);
                             break;
    case 3:   strPhoto3 = Base64.encodeToString(b, Base64.NO_WRAP);
        ivAttachment3.setImageBitmap(bm);
                             break;
    case 4:
        strPhoto4 = Base64.encodeToString(b, Base64.NO_WRAP);
        ivAttachment4.setImageBitmap(bm);
                             break;
         case 5:
        strPhoto5 = Base64.encodeToString(b, Base64.NO_WRAP);
        ivAttachment5.setImageBitmap(bm);
                             break;
        default :
        break;
        }
            } 
        }

Upvotes: 1

Related Questions