【问题标题】:Can't get bitmap to upload to php server?无法将位图上传到 php 服务器?
【发布时间】:2013-03-10 22:22:51
【问题描述】:

我正在尝试让我的应用将使用相机拍摄的图像上传到服务器。我知道这不是服务器,因为我可以毫无问题地访问该 url。我收到Error in http connection android.os.NetworkOnMainThreadException 作为错误。这是我正在尝试使用的代码。

public void uploadFile(Bitmap file) {
        Bitmap bitmapOrg = file;
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
        byte[] ba = bao.toByteArray();
        String ba1=Base64.encodeBytes(ba);

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("image",ba1));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://example.info/appserver/upload.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }

    }

【问题讨论】:

    标签: android image upload


    【解决方案1】:

    网络活动不能在主线程(UI 线程)上完成,因为它会极大地影响您的应用程序对用户的感觉。查看training doc on doing network access using AsyncTasks 以获得解决方案。

    【讨论】:

      猜你喜欢
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 2017-05-04
      • 2011-05-30
      • 2015-07-24
      相关资源
      最近更新 更多