在活动A中,它有3个listView
。单击提交按钮时,我想将文本和imagePath存储到MySQL中,并将图像存储在PhotoUpload文件夹中。
String imagess;
Uri imgURI;
public void uploadImageAndText(ArrayList<ImageAndText> listItems, final String id) {
JSONArray jsonArray = new JSONArray();
try {
for (ImageAndText i : listItems) {
JSONObject object = new JSONObject();
String type = i.getType();
String[] Type = type.split(":");
object.put("type", Type[1]);
Toast.makeText(getApplicationContext(), Type[1], Toast.LENGTH_LONG).show();
String amount = i.getAmount();
String[] Amount = amount.split(":");
object.put("amount", Amount[1]);
String description = i.getDescription();
String[] Description = description.split(":");
object.put("description", Description[1]);
Uri uploadImage = i.getImage(); //not sure
object.put("image", uploadImage); // not sure
object.put("ts_id", id);
jsonArray.put(object);
}
} catch (JSONException e) {
e.printStackTrace();
}
AddStaff ru = new AddStaff(jsonArray);
ru.execute();
}
class AddStaff extends AsyncTask<String, Void, String> {
ProgressDialog loading;
JSONArray jsonArray;
AddStaff(JSONArray jsonArray) {
this.jsonArray = jsonArray;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(AddClaims.this, "Please Wait", null, true, true);
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String, String>();
data.put("listItems", jsonArray.toString());
data.put(Configs.KEY_IMAGE,imagess); // not sure
RequestHandler rh = new RequestHandler();
String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
}
staffBenefit.php
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
if( !empty( $_POST['listItems'] ) ){
$mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
if( $mysqli->connect_errno ) echo "Failed to connect to MySQL";
$image = $_POST['image'];
$listItems = json_decode( $_POST['listItems'], true );
$sql="SELECT id FROM staff_benefit ORDER BY id ASC";
$id=0;
$res=$mysqli->query( $sql );
while( $rs=$res->fetch_object() ) $id=$rs->id;
$path="$id.png";
$actualpath="http://192.168.107.115:80/Android/CRUD/PhotoUpload/$path";
$sql="INSERT INTO `staff_benefit` ( `type`, `amount`, `description`, `image`, `ts_id` ) VALUES ( ?, ?, ?, ?, ? )";
$stmt=$mysqli->prepare( $sql );
$pathelements=array( realpath( $_SERVER['DOCUMENT_ROOT'] ), 'CRUD', 'PhotoUpload', '' );
$savepath = realpath( implode( DIRECTORY_SEPARATOR, $pathelements ) ) . "{$id}.png";
$bytes=file_put_contents( $savepath, base64_decode( $image ) );
if( !$bytes ){
echo 'Error saving image';
}
if ( $stmt ) {
foreach( $listItems as $item ){
$stmt->bind_param('sssss', $item['type'], $item['amount'], $item['description'], $actualpath, $item['ts_id'] );
$res=$stmt->execute();
if( !$res ) echo 'Query failed with code: '.$stmt->errno;
}
}
$mysqli->close();
}
}
?>
我的应用程序没有崩溃,但我在logCat中看到了这个。
01-14 02:05:11.978 20483-21081/com.example.project.myapplication W/System.err﹕ java.lang.NullPointerException
01-14 02:05:11.978 20483-21081/com.example.project.myapplication W/System.err﹕ at libcore.net.UriCodec.encode(UriCodec.java:132)
01-14 02:05:11.978 20483-21081/com.example.project.myapplication W/System.err﹕ at java.net.URLEncoder.encode(URLEncoder.java:57)
01-14 02:05:11.978 20483-21081/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.Handler.RequestHandler.getPostDataString(RequestHandler.java:118)
01-14 02:05:11.978 20483-21081/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.Handler.RequestHandler.sendPostRequest(RequestHandler.java:51)
01-14 02:05:11.978 20483-21081/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.AddClaims$AddInfo$AddStaff.doInBackground(AddClaims.java:480)
01-14 02:05:11.978 20483-21081/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.AddClaims$AddInfo$AddStaff.doInBackground(AddClaims.java:459)
有人可以帮助我如何实现这一目标吗?如何将URI图像存储到文件夹中?感谢。
被修改
@Override
protected String doInBackground(String... params) {
for (int index = 0; index < jsonArray.length(); index++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String strUri = jsonObject.getString("image");
HashMap<String, String> data = new HashMap<String, String>();
data.put("listItems", jsonArray.toString());
data.put(Configs.KEY_IMAGE, getStringImage(Uri.parse(strUri)));
RequestHandler rh = new RequestHandler();
String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
return result;
} catch (Exception e) {
}
}
return "";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
public String getStringImage(Uri imgUri) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
} catch (Exception e) {
}
return "";
}
}
当我检查MySQL时,插入了三个数据,但存储在列图像中的路径是相同的(最后一个),只有最后一个图像存储在photoUpload文件夹中。
它假设有36.png,37.png和38.png,但它只保存了36.png
答案 0 :(得分:2)
将图像从android发送到MySQL服务器
获取NPE
,因为imagess
数组为null
。
因为想要从设备向服务器发送多个图像。迭代JSONArray,在发送到服务器之前发送doInBackground
并从Uri获取Bitmap
:
1。在uploadImageAndText
方法存储中,Uri为JSONObject.Change中的字符串:
object.put("image", uploadImage);
到
object.put("image", uploadImage.toString());
2. :使用Uri和编码图像字符串创建一个获取位图的方法,如:
public String getStringImage(Uri imgUri){
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imgUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
3。最后在getStringImage
中致电doInBackground
:
@Override
protected String doInBackground(String... params) {
for(int index=0;index<jsonArray.length();index++){
JSONObject jsonObject=jsonArray.getJSONObject(index);
String strUri=jsonObject.getString("image");
HashMap<String, String> data = new HashMap<String, String>();
data.put("listItems", jsonArray.toString());
data.put(Configs.KEY_IMAGE,getStringImage(Uri.parse(strUri)));
RequestHandler rh = new RequestHandler();
String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
}
return "";
}