防止位图太大而无法上传到纹理android

Pra*_*tik 15 android android-bitmap

我需要以图库形式全屏显示原始图像.对于拇指,它将完美地工作,当我尝试使用原始源以全屏显示该图像时,它将无法显示.在大多数情况下,如果图像分辨率大于2000,那么它将显示错误位图太大而无法上传到纹理android.

我想阻止这一点,我搜索谷歌但没有得到任何答案.

Phi*_*o99 17

我遇到了同样的问题,用了这个问题的一个解决方案班轮想出了这里:

Picasso.with(context).load(new File(path/to/File)).fit().centerCrop().into(imageView);
Run Code Online (Sandbox Code Playgroud)


小智 8

我刚刚创建了一个if else函数来检查图像是否大于1M像素这里是示例代码:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

   if (resultCode == RESULT_OK) {

     if (requestCode == SELECT_PICTURE) {

        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
        int height = bitmap.getHeight(), width = bitmap.getWidth();

        if (height > 1280 && width > 960){
            Bitmap imgbitmap = BitmapFactory.decodeFile(selectedImagePath, options);
            imageView.setImageBitmap(imgbitmap);

            System.out.println("Need to resize");

        }else {
            imageView.setImageBitmap(bitmap);
            System.out.println("WORKS");
        }
Run Code Online (Sandbox Code Playgroud)


she*_*hem 1

您不需要加载整个图像,因为它太大,并且您的手机可能无法显示完整的位图像素。您需要先根据您的设备屏幕尺寸对其进行缩放。这是我发现的最好的方法,效果非常好: Android: Resize a large bitmap file to scaled output file