Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BitmapFactory.decode文件返回空值_Android_Bitmap_Bitmapfactory - Fatal编程技术网

Android BitmapFactory.decode文件返回空值

Android BitmapFactory.decode文件返回空值,android,bitmap,bitmapfactory,Android,Bitmap,Bitmapfactory,我将使用Bitmap.compress()方法压缩图像 但是当我使用Bitmap=BitmapFactory.decodeFile()获得Bitmap时,我得到一个null对象,并且该方法没有任何异常 这是我的密码 public static File compressImage(String imagePath) throws IOException { // Get bitmap BitmapFactory.Options options = new BitmapFacto

我将使用
Bitmap.compress()
方法压缩图像

但是当我使用
Bitmap=BitmapFactory.decodeFile()
获得
Bitmap
时,我得到一个
null
对象,并且该方法没有任何异常

这是我的密码

public static File compressImage(String imagePath) throws IOException {

    // Get bitmap
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

    // Get bitmap output stream
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

......
当代码在最后一行运行时,我得到一个
NullPointerException

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
然后我在调试模式下运行代码,结果是我从
BitmapFactory.decodeFile
方法中得到了一个
null
对象

参数
imagePath

/存储/仿真/0/DCIM/Camera/IMG_20160610_195633.jpg

看起来还可以


这段代码在另一个活动中运行良好,但当我将其复制到一个异步线程(我试图压缩和上载图像)时,它崩溃了。有没有可能是因为异步线程的原因?或者其他我没有注意到的东西?

从代码中删除以下内容:

options.inJustDecodeBounds = true;
inJustDecodeBounds
的文档中:

如果设置为true,解码器将返回null(无位图),但 出来字段仍将被设置,允许调用方查询 位图,而无需为其像素分配内存


inJustDecodeBounds
对于高效加载大型
位图非常有用,因为您可以读取它们的尺寸,而无需为它们分配内存,尽管它在代码中没有任何用途。

如果您使用通过USB连接的手机调试代码,则此答案适用。我的手机显示Android版本6.0.1

我读了所有关于设置“使用权限”字段的帖子,但我的代码仍然不起作用。但是,我发现我需要在“设置->设备->应用程序->应用程序管理器”下进入我的设备,然后单击我正在调试的应用程序,导航到“权限”,并手动启用请求的权限

以下权限必须位于AndroidManifest.xml中

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />