Как скопировать изображение перед загрузкой с Android? - PullRequest
0 голосов
/ 11 июня 2019

Я загружаю некоторые изображения через мое приложение, некоторые изображения из-за их размера не могут быть загружены ... Как решить эту проблему?Пожалуйста, помогите

 if (requestCode == pickImageCode && resultCode == Activity.RESULT_OK && data != null) {

            table = data.data
            val arrayOfData = arrayOf(MediaStore.Images.Media.DATA)
            val myImageQuery = view!!.context.contentResolver.query(table, arrayOfData, null, null, null)
            myImageQuery.moveToFirst()
            val columnIndex = myImageQuery.getColumnIndex(arrayOfData[0])
            imagePath = myImageQuery.getString(columnIndex)
            myImageQuery.close()
            val myImage = BitmapFactory.decodeFile(imagePath)
            imageToInSendLayout.setImageBitmap(myImage)


        } else {
            return
        }

        imageUri = data.data

Предварительный просмотр изображения в imageView перед отправкой на сервер

Ответы [ 2 ]

0 голосов
/ 11 июня 2019

То, что вы могли бы сделать, это (извините за версию Java, у меня нет kotlin версии :)):

BitmapFactory.Options bounds = new BitmapFactory.Options();
        bounds.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, bounds);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = calculateInSampleSize(bounds, reqWidth, reqHeight);
        Bitmap bm = BitmapFactory.decodeFile(imagePath, opts);

, где

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int inSampleSize = 1;
        if (reqWidth != 0 & reqHeight != 0) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;

            if (height > reqHeight || width > reqWidth) {

                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) >= reqHeight
                        && (halfWidth / inSampleSize) >= reqWidth) {
                    inSampleSize *= 2;
                }
            }
        }
        return inSampleSize;
    }

Что происходит: вы декодируете данные файла, затем, передавая reqwidth / reqheight, уменьшаете его. Затем вы можете передать его в свой макет:)

0 голосов
/ 11 июня 2019

Вы можете использовать lib Picasso:

Picasso
    .with(context)
    .load(path)
    .resize(sizeW, sizeH)
    .centerCrop()
    .into(target)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...