方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream)

时间:2019-05-03 12:07:44

标签: java android kotlin bitmap

我的代码中出现此错误,有人通知我如何解决?谢谢所有能提供帮助的人。

 <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>/Users/myUserName/learning-maven/child-module1/target/site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>/Users/myUserName/learning-maven/child-module1/target/site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>/Users/myUserName/learning-maven/child-module1/target/site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>

线路错误

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
        at map.app.fragments.ReportFragment.putImgToBytearray(ReportFragment.kt:177)

代码

bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream)

onActivityResult代码

private fun putImgToBytearray(): ByteArray {
    val stream = ByteArrayOutputStream()
    val drawable = this.imgThumb!!.drawable as BitmapDrawable
    val bitmap = drawable.bitmap
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream)
    return stream.toByteArray()
}

从图库中选择图像的方法。只是改编

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {


        try {

            //Getting the Bitmap from Gallery
            val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?
            this.imgThumb!!.setImageBitmap(bitmap)
            this.pictureTaken = true
        } catch (e:IOException) {
            e.printStackTrace()
        }
    } else {
        Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)
    }
}

1 个答案:

答案 0 :(得分:0)

我敢打赌MediaStore.Images.Media.getBitmap无法正确获取位图并返回null。

因此,如果您删除?中的MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?,您将得到一个运行时异常,正如您先前的问题所断言的那样。

因此将演员表更改为:

MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap

为确保您永远不会获得空位图,请返回调查getBitmap方法

我不知道createImageFile方法的作用,但我建议您简单地执行以下操作以测试其是否有效:

MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse("file://"+uri));

最后,我建议完全不要使用MediaStore.Images.Media.getBitmap

请参见Its deprecated。切换到图像加载库或使用 ImageDecoder#createSource(ContentResolver, Uri)

在提到的链接中有一些指南。像这样一个:

public static Bitmap decodeBitmap (ImageDecoder.Source src)

还要确保在工作线程中执行这些操作。