我正在开发一个模块,用户可以在其中将图像上传到服务器。为此,我必须将所选图像更改为 Base64。转换后,我必须使用 Json POST 方法上传图像,但每次应用程序崩溃并且 Logcat 都会出现此错误
Error converting result java.lang.NullPointerException: lock == null
另一行错误是
Error parsing data org.json.JSONException: End of input at character 0 of
这是我正在尝试的代码,请看一下,让我看看我在这里犯了什么错误。
buttonLoadPicture.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
ss = BitmapFactory.decodeFile(picturePath);
Log.d("value", ss.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ss.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
encodedImage = imageEncoded.toString();
Log.e("LOOK", imageEncoded); // **working proper**
Log.e("encodedImage", encodedImage);// **working proper**
new PostImage().execute();
}
}
class PostImage extends AsyncTask<String, String, String>{
@SuppressWarnings("deprecation")
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
String encodedString = encodedImage.toString();
Log.d("stringgggg", encodedString);//**working proper**
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uploaded_file", encodedString));
String uri = "http://192.168.2.166/uploadimage/upload.php";
JSONObject json = jsonParser.makeHttpRequest(uri,"POST",params);
Log.d("valueeeeee", json.toString());// **Crashhhh!!!!!**
return json.toString();
}
}
}
这是我的 php 代码
<?php
$json["success"] = 1; $json["message"] = "YOU ARE HERE... UPLOAD.PHP";
$json['image']=$_FILES['uploaded_file']['name'];
$json['tmp_name']=$_FILES['uploaded_file']['tmp_name'];
echo json_encode($json);
die();
//print_r($_FILES); die();
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
//echo "success";
$json["success"] = 1; $json["message"] = "Successfully post save";
} else{
// echo "fail";
$json["success"] = 0; $json["message"] = "Fail post save";
}
// print_r($json);
// var_dump(json_encode($json));
echo json_encode($json);
?>