使用/ storage / emulated / 0 / Pictures /中的图片显示空对象引用错误

时间:2017-05-07 15:07:13

标签: android nullpointerexception

我有一个从Uri(相机或图库)发送图片的功能。 这正在使用相机拍摄照片并使用图库中的一些图像,例如使用日志:

  • debug: img path : file:///storage/emulated/0/fname_1494169335416.jpg
  • debug: img path : /storage/emulated/0/DCIM/OpenCamera/IMG_20170507_162430.jpg

但它没有像这样使用路径/storage/emulated/0/Pictures/...

  • debug: img path : /storage/emulated/0/Pictures/fname_1493561293693.jpeg

我有这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

这是我的postFile()功能:

public static void postFile(Context context, String url, String fileUri, AsyncHttpResponseHandler responseHandler) {
        if (myCookieStore == null)
        {
            myCookieStore = new PersistentCookieStore(context);
            client.setCookieStore(myCookieStore);
        }
        client.setSSLSocketFactory(MySSLSocketFactory.getFixedSocketFactory());

        File myFile = new File(Uri.parse(fileUri).getPath());

        Log.d("debug", "img path : " + fileUri);

        File compressedImageFile = new Compressor.Builder(context)
                .setMaxWidth(1920)
                .setMaxHeight(1200)
                .setQuality(35)
                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES).getAbsolutePath())
                .build()
                .compressToFile(myFile);

        RequestParams params = new RequestParams();
        try {
            params.put("profile_picture", compressedImageFile);
        } catch(FileNotFoundException e) {
            Log.d("error", "error catch");
        }
        Log.d("absolute url", "" + "*" + getAbsoluteUrl(url) + "*");
        client.post(context, getAbsoluteUrl(url), params, responseHandler);
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案: 这是因为我在压缩后没有删除我的文件,因此当我想压缩之前压缩的文件时,它会崩溃。 所以我放了file.delete()