首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在使用android上传图片之前复制图片?

如何在使用android上传图片之前复制图片?
EN

Stack Overflow用户
提问于 2019-06-12 01:28:11
回答 2查看 22关注 0票数 -1

我正在上传一些图片通过我的应用程序,一些图片,因为他们的大小无法上传…如何解决这个问题?请帮帮忙

代码语言:javascript
复制
 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格式

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-12 03:12:22

您可以做的是(对不起java版本,我没有kotlin版本: ):):

代码语言:javascript
复制
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);

哪里

代码语言:javascript
复制
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;
    }

然后,您可以将其传递给您的布局:)

票数 0
EN

Stack Overflow用户

发布于 2019-06-12 01:33:10

你可以使用库毕加索:

代码语言:javascript
复制
Picasso
    .with(context)
    .load(path)
    .resize(sizeW, sizeH)
    .centerCrop()
    .into(target)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56548915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档