在imageview android中设置多张照片

时间:2016-08-31 02:29:52

标签: android image android-studio upload imageview

没有我建立一些图片库(附件),用户可以上传5张照片。 所以我想使用ActivityResult在imageview中设置多张照片 要做到这一点,你必须从每个imageview上传它

这是我的代码

 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);

    }
}

这是我的布局代码

<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>

但问题是当我在ivAttachment1上传时,其他人会在ivAttachment2,3,4和5中的同一张图片中上传。我需要创建某种&#34;开关&#34;也许方法。而且我必须将每个位图照片都放到字符串中,这样我就可以将它保存到我的数据库中。 你能告诉我最好的方式吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您必须保持单击哪个ImageView,然后在onActivityResult中检查单击的ImageView,然后执行进一步操作。

e.g。在你的类中创建一个枚举或整数变量:

 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;
        }
            } 
        }