【问题标题】:Uploading image file works on emulator but not woring on device上传图像文件适用于模拟器但不适用于设备
【发布时间】:2016-02-23 19:56:44
【问题描述】:

我正在尝试将图像文件上传到服务器,当我在模拟器上尝试时这工作正常,但是从设备上传时它出现内部服务器错误

我遵循的步骤是:
1] 从相机捕获图像并将其存储在外部存储中。
2] 在imageview上显示文件。
3] 在按钮上单击将文件从外部存储上传到服务器。

当点击上传按钮时,我已经编写了代码。

Category=(String) category.getSelectedItem();
Bitmap sendbitmap = rotateBitmapImage(BitmapFactory.decodeFile(mCurrentPhotoPath));

ByteArrayOutputStream stream = new ByteArrayOutputStream();
sendbitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
 //compress to which format you want.

 byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);

final ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair("userid",Integer.toString(userid)));
nameValuePairs.add(new BasicNameValuePair("State",State));
nameValuePairs.add(new BasicNameValuePair("rc",rc));
nameValuePairs.add(new BasicNameValuePair("vill",vill));
nameValuePairs.add(new BasicNameValuePair("Category",Category));           
new UploadData().execute(nameValuePairs);

上传数据异步类:

private class UploadData extends AsyncTask<ArrayList<NameValuePair>, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(FinalStage.this);
                pDialog.setMessage("Uploading data");
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected Void doInBackground(ArrayList<NameValuePair>... arg0) {

                try{
                    HttpClient httpclient = new DefaultHttpClient();
                   HttpPost("http://xxx.xxx.com/webServices/upload_image.php");
                    httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    final String response = httpclient.execute(httppost,responseHandler);
                    System.out.println("response "+response);
                    JSONArray jlogin = new JSONArray(response);                        
                     JSONObject loginObj = (JSONObject) jlogin.getJSONObject(0);
                     if(loginObj.getString("status").equals("true"))
                     {
                         runOnUiThread(new Runnable() {


                               @Override
                               public void run() {
                                 File f=new File(mCurrentPhotoPath);
                                     f.delete();
                                     img.setImageResource(R.drawable.ic_launcher);
                                   Toast.makeText(FinalStage.this, "Uploaded Successfully ", Toast.LENGTH_LONG).show();                          
                               }
                           });
                     }
                     else
                     {
                         runOnUiThread(new Runnable() {


                               @Override
                               public void run() {
                                   Toast.makeText(FinalStage.this, " Problem upload Uploaded", Toast.LENGTH_LONG).show();                          
                               }
                           });

                     }


                }catch(final Exception e){
                     runOnUiThread(new Runnable() {

                       @Override
                       public void run() {
                           System.out.println("Soham "+e.getMessage());
                           Toast.makeText(FinalStage.this, e.getMessage(), Toast.LENGTH_LONG).show();                              
                       }
                   });

                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                if (pDialog.isShowing())
                    pDialog.dismiss();

            }

        }

PHP 脚本:

include_once("connection.php");
    $base=$_REQUEST['image'];
    $userid=$_REQUEST['userid'];
    $state=$_REQUEST['State'];
    $rc=$_REQUEST['rc'];
    $vill=$_REQUEST['vill'];
    $Category=$_REQUEST['Category'];

    $date_dir=date("Y-m-d");
    $time_offset="525";
    $time_a=($time_offset * 120);
    $system_time=date('h-i-s',time() + $time_a);

    $querymax="select IFNULL(MAX(id),0)+1 as id from record";
    $stmt = $con->prepare($querymax);
    $stmt->execute();
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $id=$row['id'];
    //echo $id;

    $imagename=$state."_".$vill."_".$Category."_".$id.".jpg";

    //$date_dir="12-12-2014";
    //$area_dir="sambhaji";
    //$dirname="uploads/";
    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');


    //$w=mysql_query("INSERT into image values('$login_id','$name',10)");
    $url="webServices/uploads";

    chdir("uploads");
    if(!file_exists($date_dir))
    {
        mkdir($date_dir);
        chdir($date_dir);
        if(!file_exists($state))
        {
            mkdir($state);
            chdir($state);
            if(!file_exists($Category))
            {
                mkdir($Category);
                chdir($Category);

                $file = fopen($imagename, 'wb');
                fwrite($file, $binary);
                fclose($file);

                $path="$url/$date_dir/$state/$Category/";

                $query="insert into record (state,cycle_rout,village,category,image_name,userid,date)values(:state,:rc,:vill,:Category,:imagename,:userid,:date_dir)";
                $stmt = $con->prepare($query);
                $stmt->bindParam(':state',$state);
                $stmt->bindParam(':rc',$rc);
                $stmt->bindParam(':vill',$vill);
                $stmt->bindParam(':Category',$Category);
                $stmt->bindParam(':imagename',$imagename);
                $stmt->bindParam(':userid',$userid);
                $stmt->bindParam(':date_dir',$date_dir);

                if($stmt->execute())
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'true');
                    print(json_encode($json_output));
                }
                else
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'false');
                    print(json_encode($json_output));
                }
            }
            else
            {   
                chdir($Category);

                $file = fopen($imagename, 'wb');
                fwrite($file, $binary);
                fclose($file);

                $path="$url/$date_dir/$state/$Category/";

                $query="insert into record (state,cycle_rout,village,category,image_name,userid,date)values(:state,:rc,:vill,:Category,:imagename,:userid,:date_dir)";
                $stmt = $con->prepare($query);
                $stmt->bindParam(':state',$state);
                $stmt->bindParam(':rc',$rc);
                $stmt->bindParam(':vill',$vill);
                $stmt->bindParam(':Category',$Category);
                $stmt->bindParam(':imagename',$imagename);
                $stmt->bindParam(':userid',$userid);
                $stmt->bindParam(':date_dir',$date_dir);
                if($stmt->execute())
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'true');
                    print(json_encode($json_output));
                }
                else
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'false');
                    print(json_encode($json_output));
                }

            }           
        }
        else
        {
            chdir($state);
            if(!file_exists($Category))
            {
                mkdir($Category);
                chdir($Category);

                $file = fopen($imagename, 'wb');
                fwrite($file, $binary);
                fclose($file);

                $path="$url/$date_dir/$state/$Category/";

                $query="insert into record (state,cycle_rout,village,category,image_name,userid,date)values(:state,:rc,:vill,:Category,:imagename,:userid,:date_dir)";
                $stmt = $con->prepare($query);
                $stmt->bindParam(':state',$state);
                $stmt->bindParam(':rc',$rc);
                $stmt->bindParam(':vill',$vill);
                $stmt->bindParam(':Category',$Category);
                $stmt->bindParam(':imagename',$imagename);
                $stmt->bindParam(':userid',$userid);
                $stmt->bindParam(':date_dir',$date_dir);
                if($stmt->execute())
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'true');
                    print(json_encode($json_output));
                }
                else
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'false');
                    print(json_encode($json_output));
                }
            }
            else
            {   
                chdir($Category);

                $file = fopen($imagename, 'wb');
                fwrite($file, $binary);
                fclose($file);

                $path="$url/$date_dir/$state/$Category/";

                $query="insert into record (state,cycle_rout,village,category,image_name,userid,date)values(:state,:rc,:vill,:Category,:imagename,:userid,:date_dir)";
                $stmt = $con->prepare($query);
                $stmt->bindParam(':state',$state);
                $stmt->bindParam(':rc',$rc);
                $stmt->bindParam(':vill',$vill);
                $stmt->bindParam(':Category',$Category);
                $stmt->bindParam(':imagename',$imagename);
                $stmt->bindParam(':userid',$userid);
                $stmt->bindParam(':date_dir',$date_dir);
                if($stmt->execute())
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'true');
                    print(json_encode($json_output));
                }
                else
                {
                    header('Content-Type: application/json');
                    $json_output[]=array('status'=>'false');
                    print(json_encode($json_output));
                }

            }   
        }
    }

为什么会这样?在模拟器上它工作得很好,但在设备上它给出了问题。它卡在进度条上,2 3 分钟后它显示 Toast 消息为 Internal Server Error

【问题讨论】:

  • 你能发布异常的堆栈跟踪吗?
  • 你的服务器在哪里?以及客户端使用哪个ip连接服务器?
  • '不显示 Toast 消息为请检查 Internet 连接!!内部服务器错误'。那是你自己的话。你有一个问题。更好地告诉 e.getMessage() 记录了什么。还有 Toast e.getMessage() 而不是你自己的 -wrong- 描述。
  • @octavianoPutra:它进入 UploadData 并启动进度条之后,当我在 samsung j7 上尝试这个但当我在 samsung grand 上尝试它时,什么都没有发生。它有效。
  • @greenapps:对不起,这是错误的消息。这只是为了我的理解。并且使用 e.getMessage() 记录内部服务器错误。

标签: php android android-asynctask


【解决方案1】:

实际上问题在于使用 base64 编码来传输高质量的图像。我试图上传来自相机的原始图像。我用 MultipartEntity 替换了 base64 编码传输。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 2011-03-08
    • 2015-02-13
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多