我正在尝试将捕获/选定的图像从库上传到我的服务器,但是它给出了以下错误

时间:2016-02-16 09:38:43

标签: android android-asynctask upload server

 class ImageGalleryTask extends AsyncTask<Void, Void, String> {
    @SuppressWarnings("unused")
    @Override
    protected String doInBackground(Void... unsued) {
            InputStream is;
            BitmapFactory.Options bfo;
            Bitmap bitmapOrg;
            ByteArrayOutputStream bao ;
           // ImageView imgshoot = null;
            bfo = new BitmapFactory.Options();
            bfo.inSampleSize = 2;
           // bitmapOrg =   BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + customImage, bfo);
    //      bitmap = mark(bitmap, "Hallo");



            bao = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 0, bao);
            byte [] ba = bao.toByteArray();
            Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bao.toByteArray()));
            String ba1 = Base64.encodeBytes(ba);
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("image",ba1));
            nameValuePairs.add(new BasicNameValuePair("cmd","image_android"));
            Log.v("log_tag", System.currentTimeMillis()+".png");           
            try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new 
                  //  Here you need to put your server file address
                    HttpPost("http://192.168.0.1/Upload/UploadImg.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                    Log.v("log_tag", "In the try Loop" );
               }catch(Exception e){
                    Log.v("log_tag", "Error in http connection "+e.toString());
               }
        return "Success";
        // (null);
    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

    @Override
    protected void onPostExecute(String sResponse) {
        try {
            if (dialog.isShowing())
                dialog.dismiss();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }

            imgView.setImageBitmap(bitmap);
}

这是Logcat中显示的错误:

  

02-16 14:44:05.872:E / AndroidRuntime(9375):   java.lang.RuntimeException:执行时发生错误   doInBackground()02-16 14:44:05.872:E / AndroidRuntime(9375):at
  android.os.AsyncTask $ 3.done(AsyncTask.java:309)

     

02-16 14:44:05.872:E / AndroidRuntime(9375):at   java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)   02-16 14:44:05.872:E / AndroidRuntime(9375):at   java.util.concurrent.FutureTask.setException(FutureTask.java:223)   02-16 14:44:05.872:E / AndroidRuntime(9375):at   java.util.concurrent.FutureTask.run(FutureTask.java:242)02-16   14:44:05.872:E / AndroidRuntime(9375):at   android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)02-16   14:44:05.872:E / AndroidRuntime(9375):at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)   02-16 14:44:05.872:E / AndroidRuntime(9375):at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:588)   02-16 14:44:05.872:E / AndroidRuntime(9375):at   java.lang.Thread.run(Thread.java:818)02-16 14:44:05.872:   E / AndroidRuntime(9375):引起:   android.view.ViewRootImpl $ CalledFromWrongThreadException:只有   创建视图层次结构的原始线程可以触及其视图。   02-16 14:44:05.872:E / AndroidRuntime(9375):at   android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6603)02-16   14:44:05.872:E / AndroidRuntime(9375):at   android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:954)

我甚至添加了存储在php文件中的服务器代码:(check.php):

<?php
    $target_dir = "Upload/";
    $base=$_REQUEST['image'];
    $name=$_REQUEST['cmd'];
    $binary=base64_decode($base);
      header('Content-Type: jpeg; charset=utf-8');
   $file = fopen($target_path, 'wb');
   $file = fopen($name, 'wb');
   fwrite($file, $binary);
   fclose($file);
    //move_uploaded_file($file,$target_path.$name);
      copy($name,$target_path.$name);
     ?>

2 个答案:

答案 0 :(得分:1)

替换imgView.setImageBitmap(位图);与

runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        imgView.setImageBitmap(bitmap);
                    }
                });

答案 1 :(得分:0)

在doInBackground()中,您编写了imgView.setImageBitmap(bitmap)这是不可接受的。您应该在该方法中编写后台操作。

有很多教程。希望它会有所帮助。 试试这个example