E/Volley: [5836] NetworkDispatcher.run: Unhandled exception
java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.String.length()' on a null object reference
这是我的代码:
private ImageView imgNBI;
private Bitmap bitmap;
private String image, name, imgtype, retrieveDriverID,driverID;
private String KEY_DRIVERID = "driverid";
private String KEY_IMAGE = "image";
private String KEY_IMAGETYPE = "imgtype";
private String KEY_NAME = "name";
private Context ctx = this;
ProgressDialog pDialog;
Context context = this;
private String UPLOAD_URL ="http://angkas.net23.net/uploadimage1.php";
这是我的更新代码:
private void update() {
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
getUser();
retrieveDriverID = driverID;
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pDialog.dismiss();
Toast.makeText(Requirements_1.this, "This is response: "+response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
Toast.makeText(Requirements_1.this, "This is error: "+error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
image = getStringImage(bitmap);
String ddrid = retrieveDriverID;
String drname = name;
Map<String, String> params = new HashMap<>();
params.put(KEY_DRIVERID, ddrid);
if(ddrid != null){params.put(KEY_DRIVERID,ddrid);}
params.put(KEY_IMAGETYPE, "NBIImg");
params.put(KEY_IMAGE, image);
if(image != null){params.put(KEY_IMAGE,image);}
params.put(KEY_NAME, drname);
if(drname != null){params.put(KEY_NAME,drname);}
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
当我点击imageView:
时public void clicknbi(View view) {
showFileChooser("1");
}
private void showFileChooser(String request) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
这是活动结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imgNBI.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
转换图片:
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
这是我的PHP:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$RegistrationDate = date('Y-m-d');
$driverid = $_POST['driverid'];
$imgtype = $_POST['imgtype'];
$image = $_POST['image'];
$name = $_POST['name'];
require_once('dbcon.php');
$pathname = "$name$imgtype";
$path = "Images/$pathname.png";
$actualpath = "http://angkas.net23.net/$path";
//$sql = "INSERT INTO driverregistration (DriverID,RegistrationDate,EnglishProficiency,TagalogProficiency,NBIImg) VALUES ('$driverid','$RegistrationDate','','','$actualpath');";
$sql = "UPDATE driverregistration SET NBIImg = '$actualpath' WHERE DriverID = $driverid;";
if(mysqli_query($con,$sql))
{
file_put_contents($path,base64_decode($image));
echo "Successfully Registered";
}
else
{
echo "Could not insert image";
}
}
else
{
echo 'error';
}
?>
我遇到两个错误。第一个是空对象引用,第二个错误是:
java.net.SocketException: sendto failed: EPIPE (Broken pipe)
我在Toast中收到此错误。