androidmethodsimageviewclicklistener

Displaying two images from android device


 @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_adharFrontUploadId:
                pickImage();
                loadAdharFrontImage();
                break;
            case btn_adharBackUploadId:
                pickImage();
                loadAdharBackImage();
                break;
        }
    }

it displays two different images from device. I wrote the method as follows

 private void loadAdharBackImage() {
        new Thread() {
            public void run() {
                while (i++ < 1000) {
                    try {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                iv_adharBack.setImageBitmap(decodedWebP);
                            }
                        });
                        sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    private void loadAdharFrontImage() {
        new Thread() {
            public void run() {
                while (i++ < 1000) {
                    try {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                    iv_adharFront.setImageBitmap(decodedWebP);
                            }
                        });
                        sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

when i select the first image by clicking on first button, it loads the first imageview as usual, but when i click the second button, it loads the first selected image in the second imageview instantaneously, and upon select the second image, the first imageview also changes to the second selected image.Please give me a solution for this. i am very new to android and programming. Any help will be appreciated


Solution

  • Thank you for the support folks...finally i got answer. No need to use the runnable mentioned in my question. Here i can make the code to fetch images for different imageViews. Thanks again

    @Override
       public void onClick(View v) {
           switch (v.getId()) {
               case btn_applAdharFrontUploadId:
                   pickImage(1);
                   break;
               case btn_applAdharBackUploadId:
                   pickImage(2);
                   break;
           }
       }
    

    Here i can use the same code to compress my images and load them to imageViews

    private void pickImage(int i) {
    
            Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
            pickImageIntent.setType("image/*");
            startActivityForResult(Intent.createChooser(pickImageIntent, "Select Picture"), i);
    
        }
    @Override
        public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
    
                assert data != null;
                Uri imageUri = data.getData();
                InputStream is = null;
                try {
                    assert imageUri != null;
                    is = getContentResolver().openInputStream(imageUri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                assert is != null;
                BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
                Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.WEBP, 60, out);
                Bitmap decodedImage = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
                switch (requestCode){
                    case 1:
                        iv_ApplAdharFront.setImageBitmap(decodedImage);
                        break;
                    case 2:
                        iv_ApplAdharBack.setImageBitmap(decodedImage);
                        break;