Volley位图压缩-> NetworkDispatcher.run:未处理的异常java.lang.NullPointerException:....关于空对象引用

时间:2018-08-13 13:19:30

标签: image bitmap null android-volley png

使用位图获得了一些问题,并且不知道该怎么做。使用null对象每次都会成为一个问题引用

  

[29547] NetworkDispatcher.run:未处理的异常   java.lang.NullPointerException:尝试调用虚拟方法   '布尔   android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,   int,java.io.OutputStream)'在空对象引用上       java.lang.NullPointerException:尝试调用虚拟方法'boolean   android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,   int,java.io.OutputStream)'引用空对象

     

在com.example.viktor.doggy010.Util.HelperMethods.getImageAsString(HelperMethods.java:25)           在com.example.viktor.doggy010.RegisterActivity $ 5.getParams(RegisterActivity.java:193)           在com.android.volley.Request.getBody(Request.java:397)           在com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)           在com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)           在com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)           在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)           在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)

RegisterActivity:

@Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);
            Bitmap temp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

            String image = HelperMethods.getImageAsString(temp);
            params.put("image",image);

            return params;
        }

HelperMethods:

public static Bitmap getImageAsBitmap(String image){
    byte[] imageAsBytes = Base64.decode(image, Base64.DEFAULT);
    Bitmap bmp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
    return bmp;
}

public static String getImageAsString(Bitmap bmp){
    ByteArrayOutputStream bYtE = new ByteArrayOutputStream();

    bmp.compress(Bitmap.CompressFormat.JPEG, 100, bYtE);
    byte[] byteArray = bYtE.toByteArray();
    String image= Base64.encodeToString(byteArray, Base64.DEFAULT);
    bmp.recycle();
    Log.d("Converted TO : ", image);
    return image;
}

我非常希望你们能帮助我。

0 个答案:

没有答案