将图像加载到图像视图时出错

时间:2018-02-19 16:08:49

标签: java android

我在ImageView中插入图片时遇到问题。 1.我插入ImageView的3.5mb图像总是在第三和第四张图像上崩溃。 2.当尺寸是应用程序时 3.大约1.5mb或更小的效果很好。 4.在第一点总是崩溃三星S7和在Oppo运行时没有问题。 程序如下,错误在哪里?谢谢你。

`I have a problem inserting the image in ImageView.

1。我插入ImageView的3.5mb图像总是在第三和第四张图像上崩溃。 2.当尺寸是应用程序时 3.大约1.5mb或更小的效果很好。 4.在第一点总是崩溃三星S7和在Oppo运行时没有问题。 程序如下,错误在哪里?谢谢你。

`    ImageView1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Please Select Image"), Image_Request_Code1);}
        //}
    });
    ImageView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Please Select Image"), Image_Request_Code2);}
    //    }
    });
    ImageView3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Please Select Image"), Image_Request_Code3);}
    //    }
    });
    ImageView4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Please Select Image"), Image_Request_Code4);}
    //    }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Image_Request_Code1 && resultCode == RESULT_OK ) {
        FilePathUri1 = data.getData();
        try {
            // Getting selected image into Bitmap.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Bitmap bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri1);
            bitmap1.compress(Bitmap.CompressFormat.JPEG,5,baos);
            byte[] BYTE1;
            BYTE1 = baos.toByteArray();
            Bitmap bitmap11 = BitmapFactory.decodeByteArray(BYTE1, 0, BYTE1.length);
            // Setting up bitmap selected image into ImageView.
            ImageView1.setImageBitmap(bitmap11);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (requestCode == Image_Request_Code2 && resultCode == RESULT_OK ) {
        FilePathUri2 = data.getData();
        try {
            // Getting selected image into Bitmap
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Bitmap bitmap2 = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri2);
            bitmap2.compress(Bitmap.CompressFormat.JPEG,5,baos);
            byte[] BYTE2;
            BYTE2 = baos.toByteArray();
            Bitmap bitmap22 = BitmapFactory.decodeByteArray(BYTE2, 0, BYTE2.length);
            ImageView2.setImageBitmap(bitmap22);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (requestCode == Image_Request_Code3 && resultCode == RESULT_OK ) {
        FilePathUri3 = data.getData();
        try {
            // Getting selected image into Bitmap.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Bitmap bitmap3 = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri3);
            bitmap3.compress(Bitmap.CompressFormat.JPEG,5,baos);
            byte[] BYTE3;
            BYTE3 = baos.toByteArray();
            Bitmap bitmap33 = BitmapFactory.decodeByteArray(BYTE3, 0, BYTE3.length);
            ImageView3.setImageBitmap(bitmap33);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (requestCode == Image_Request_Code4 && resultCode == RESULT_OK ) {
        FilePathUri4 = data.getData();
        try {
            // Getting selected image into Bitmap.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Bitmap bitmap4 = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri4);
            bitmap4.compress(Bitmap.CompressFormat.JPEG,5,baos);
            byte[] BYTE4;
            BYTE4 = baos.toByteArray();
            Bitmap bitmap44 = BitmapFactory.decodeByteArray(BYTE4, 0, BYTE4.length);
            ImageView4.setImageBitmap(bitmap44);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}'

1 个答案:

答案 0 :(得分:0)

您可以使用Picasso或Glide库将图像显示到ImageView。该库可以自动将其转换为Bitmap。

毕加索Here

Glide Here