Converting bitmap to string and compressing bitmap is worked fine in activity but in fragment activity, it generates null object reference error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.news.androidapp, PID: 5777
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 com.news.androidapp.News_Yeb.convertBitmapToString(News_Yeb.java:161)
at com.news.androidapp.News_Yeb.onClick(News_Yeb.java:110)
Extending Fragment
public class News_Yeb extends Fragment implements OnClickListener {
Button bt_register;
TextInputLayout til_name;
ImageView iv_profile;
String name, profile;
RequestQueue requestQueue;
boolean IMAGE_STATUS = false;
Bitmap profilePicture;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_yeb, container, false);
til_name = view.findViewById(R.id.til_name_reg);
iv_profile = view.findViewById(R.id.im_profile);
bt_register = view.findViewById(R.id.bt_register);
iv_profile.setOnClickListener(this);
bt_register.setOnClickListener(this);
return view;
}
Adding two onClick event using switch case, one is for imageview and another is for submit button
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.im_profile:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1000);
break;
case R.id.bt_register:
name = til_name.getEditText().getText().toString();
if ( validateName(name) )
{
final ProgressDialog progress = new ProgressDialog(getActivity());
progress.setTitle("Please Wait");
progress.setMessage("Creating Your Account");
progress.setCancelable(false);
progress.show();
convertBitmapToString(profilePicture);
RegisterRequest registerRequest = new RegisterRequest(name, profile, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Response", response);
progress.dismiss();
try {
if (new JSONObject(response).getBoolean("success")) {
Toast.makeText(getActivity().getApplicationContext(), "Account Successfully Created", Toast.LENGTH_SHORT).show();
finish();
} else
Toast.makeText(getActivity().getApplicationContext(), "Something Has Happened. Please Try Again!", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
requestQueue.add(registerRequest);
}
break;
}
}
Connecting with PHP/Mysql
public class RegisterRequest extends StringRequest {
private static final String REGISTER_URL = "..";
private Map<String, String> parameters;
public RegisterRequest(String name, String mobile, String email, String image, Response.Listener<String> listener) {
super(Method.POST, REGISTER_URL, listener, null);
parameters = new HashMap<>();
parameters.put("name", name);
parameters.put("image", image);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return parameters;
}
}
Converting bitmap image into a ByteArrayOutputStream and then converting stream into a byte array
private void convertBitmapToString(Bitmap profilePicture) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
profilePicture.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] array = byteArrayOutputStream.toByteArray();
profile = Base64.encodeToString(array, Base64.DEFAULT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == Activity.RESULT_OK && data != null) {
//Image Successfully Selected
try {
//parsing the Intent data and displaying it in the imageview
Uri imageUri = data.getData();//Geting uri of the data
InputStream imageStream = getActivity().getApplicationContext().getContentResolver().openInputStream(imageUri);//creating an imputstrea
profilePicture = BitmapFactory.decodeStream(imageStream);//decoding the input stream to bitmap
iv_profile.setImageBitmap(profilePicture);
IMAGE_STATUS = true;//setting the flag
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}