我想知道是否可以更新图像路径和图像 文件夹?
我已成功将图像路径和文本存储到android的MySQL
中,并将图像保存在目录中。
Php用于上传图片
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
if( !empty( $_POST['listItems'] ) ){
$listItems = json_decode( $_POST['listItems'], true );
$mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
if( $mysqli->connect_errno ) echo "Failed to connect to MySQL";
$sql="INSERT INTO `staff_benefit`
( `type`, `amount`, `description`, `image`, `ts_id` )
VALUES ( ?, ?, ?, ?, ? )";
if($stmt=$mysqli->prepare($sql )){
$url="http://192.168.1.7:80/Android/CRUD/PhotoUpload/";
foreach( $listItems as $item ){
$id = uniqid();
$image_name = $id.".png";
$save_path = 'PhotoUpload/'.$image_name;
$image_url = $url.$image_name;
$bytes=file_put_contents($save_path, base64_decode($item['image']));
if( !$bytes ){
echo 'Error saving image';
}else{
$stmt->bind_param('sssss',
$item['type'],
$item['amount'],
$item['description'],
$image_url,
$item['ts_id'] );
if( !$res=$stmt->execute()){
echo 'Query failed with code: '.$stmt->errno;
}
}
}
}
$mysqli->close();
}
}
?>
在 Edit_Staff_Benefit_ListView 中,将检索两行数据并将其加载到listView
。点击列表后,它会打算 Edit_Staff 进行编辑。
Edit_Staff_Benefit_ListView
listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
mClickedPosition = position; // update
long i1d = staffs.get(position).getId();
Intent intent = new Intent(getActivity(), Edit_Staff.class);
intent.putExtra("id", i1d);
intent.putExtra("ID", ID); // this is ts_id
intent.putExtra("mClickedPosition", mClickedPosition);
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
});
Edit_Staff
mClickedPosition=getIntent().getIntExtra("mClickedPosition",-1);
ID = getIntent().getLongExtra("id", 0);
IDFromInfo=getIntent().getStringExtra("ID");
Toast.makeText(getApplicationContext(), "ID" + ID+"FK"+IDFromInfo , Toast.LENGTH_LONG).show();
if (getIntent().getExtras() != null) {
RetrieveDetails(ID); // retrieve all the data based on position in listView
}
save.setOnClickListener(new View.OnClickListener() { // if save button clicked
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
claimType = spinnerType.getSelectedItem().toString();
Description = description.getText().toString();
Amount = amount.getText().toString();
if(mClickedPosition==-1)
{
//Add(claimType,Amount,Description,photo);
}
else
{
update(claimType,Amount,Description,photo);
}
}
});
更新功能
public void update( final String claimType, final String Amount, final String Description, final Uri photo)
{
class updateImageAndText extends AsyncTask<Void,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Edit_Staff.this,"Updating...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
try {
Intent returnIntent = new Intent();
returnIntent.putExtra("ClaimType", claimType);
returnIntent.putExtra("Amount", Amount);
returnIntent.putExtra("Description", Description);
returnIntent.putExtra("photo", photo);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}catch(Exception e)
{
}
}
@Override
protected String doInBackground(Void... params) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(Configs.KEY_ID,String.valueOf(ID));
hashMap.put(Configs.KEY_TYPE,claimType);
hashMap.put(Configs.KEY_AMOUNT,Amount);
hashMap.put(Configs.KEY_DESCRIPTION,Description);
hashMap.put(Configs.KEY_IMAGE,photo.toString());
RequestHandler rh = new RequestHandler();
String s = rh.sendPostRequest(Configs.URL_UPDATEDE_IMAGE_TEXT,hashMap);
return s;
}
}
Update.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$id = $_POST['id'];
$type = $_POST['type'];
$amount = $_POST['amount'];
$description = $_POST['description'];
$image = $_POST['image'];
//importing database connection script
require_once('dbConnect.php');
$sql = "UPDATE staff_benefit
SET type = '$type', amount = '$amount', description='description', image='image'
WHERE id = '$id'";
//Updating database table
if(mysqli_query($con,$sql)){
echo ' Updated Successfully';
}else{
echo mysqli_error($con);
exit;
}
//closing connection
mysqli_close($con);
}
?>
点击保存按钮时应用程序崩溃了。这是因为我写了一个错误的更新。 PHP?感谢
logcat的
01-16 19:36:51.373 22786-22786/com.example.project.myapplication E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.project.myapplication.GUI.Edit_Staff has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42c71d80 V.E..... R......D 0,0-684,324} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:467)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:289)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:104)
at com.example.project.myapplication.GUI.Edit_Staff$1updateImageAndText.onPreExecute(Edit_Staff.java:154)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.example.project.myapplication.GUI.Edit_Staff.update(Edit_Staff.java:191)
at com.example.project.myapplication.GUI.Edit_Staff$1.onClick(Edit_Staff.java:103)
答案 0 :(得分:1)
您在退出活动后尝试显示对话框。
他的错误在某些情况下可能有点误导(尽管答案仍然完全准确) - 即在我的情况下,在AsyncTask中抛出未处理的Exception,导致Activity关闭,然后打开progressdialog导致此异常..所以“真正的”例外在日志中早一点。
在退出活动之前,在您创建的对话框上调用dismiss(),例如在onPause()或onDestroy()。
原来回答HERE
答案 1 :(得分:1)
@Tony
当您收到此错误时,表示您的窗口正在泄漏进度对话框。
如果您没有取消它,就会发生这种情况,因为您的应用在此之前崩溃了。
要查看真正的错误,请删除ProgressDialog loading;
以便进行调试。
此外,您还遇到逻辑问题,您的代码应检查是否已选择图片,并将其添加到hashmap
。
如果Uri
未初始化,并且您没有检查到您将获得nullpointerException
。
您还需要像在旧问题中一样发送Base64中编码的图像。 uri是设备上文件的路径,您需要进行编码,但不能将其插入到表中。您需要重新上载新图像。
您的PHP脚本应检查传递的参数。如果照片没有更改,请不要更新该字段,如果未设置,则可以检查$_POST['image']
条件,运行没有图像列的UPDATE
查询。