douhutongvm382381 2016-01-16 11:38
浏览 53
已采纳

单击保存按钮时应用程序崩溃

I wonder is it possible to update the image path and image in the folder ?

I've been successfully stored the image path and text into MySQL from android, and save the image in a directory.

enter image description here

Php for upload image

<?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();
        }
    }
?>

In Edit_Staff_Benefit_ListView, the two row data will be retrieved and loaded into listView. When list get clicked, it will intent to Edit_Staff for edit.

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);
                }

            }
        });

update function

 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);
    }
?>

App crashed when save button is clicked.Is it because I'm wrote a wrong update. php ? Thanks

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)
  • 写回答

2条回答 默认 最新

  • dphw5101 2016-01-16 16:27
    关注

    @Tony when you get this error, it means your window is leaking the progress dialog.

    This happens when you don't cancel it, it happened because your app crashes before that.

    To see the real error remove ProgressDialog loading; for the sake of debugging.

    Also you have a logic issues, your code should check if an image has been selected or not and add it to your hashmap.

    If Uri is not initialized and you don't check that you will be getting nullpointerException.

    You also need to send the image encoded in Base64 like you did in your old question. the uri is the path to the file on your device which you need for encoding, but it is not to be inserted in your table.You need to reupload the new image instead.

    Your PHP script should check for the parameters being passed. if the photo did not change do not updated that field you can have a condition checking $_POST['image'] if it is not set run the UPDATE query without the image column.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能