【问题标题】:Unable to upload image to server in fragment android [closed]无法在片段android中将图像上传到服务器[关闭]
【发布时间】:2014-11-07 11:01:39
【问题描述】:

我正在将图像上传到按钮单击片段中的服务器。用户可以点击上传按钮,选择一张图片,然后将其发布到服务器。问题是用户从手机中选择图片,当点击发布按钮时,它没有上传到服务器。当我将此代码与 Activity 一起使用时,此代码可以完美运行。但是当我将它与片段一起使用时,它不起作用。请给出解决方案 我的代码是

image.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
            }
        });


    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (resultCode == Activity.RESULT_OK) 
        {
            if (requestCode == 1) 
            {
            // currImageURI is the global variable I’m using to hold the content:
                currImageURI = data.getData();
            }
        }
    }

        //Convert the image URI to the direct file system path of the image file
            public String getRealPathFromURI(Uri contentUri) 
            {
                String [] proj={MediaStore.Images.Media.DATA};
                android.database.Cursor cursor = getActivity().getContentResolver().query( contentUri,
                proj,     // Which columns to return
                null,     // WHERE clause; which rows to return (all rows)
                null,     // WHERE clause selection arguments (none)
                null);     // Order-by clause (ascending by name)
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }


//AsyncTask-------------------------------------------------------------------------------------------------------- 

//---------------------------using AsyncTask here------------------------------------------------------------------
            public class HttpUploader extends AsyncTask<String, Void, String> 
            {
                protected String doInBackground(String...path) 
                {
                    for (String sdPath : path) 
                    {
                        Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();

                        //Resize the image
                        double width = bitmapOrg.getWidth();
                        double height = bitmapOrg.getHeight();
                        double ratio = 400/width;
                        int newheight = (int)(ratio*height);

                        System.out.println("———-width" + width);
                        System.out.println("———-height" + height);

                        bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);

                        //Here you can define .PNG as well
                        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
                        byte[] ba = bao.toByteArray();
                        String ba1 = Base64.encodeToString(ba,Base64.DEFAULT);

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

                        try {
                            HttpClient httpclient = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost(url+"/api.upload.php?q="+URLEncoder.encode(Mobile,"UTF-8"));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                            HttpResponse response = httpclient.execute(httppost);
                            HttpEntity entity = response.getEntity();                

                            // print responce
                            outPut = EntityUtils.toString(entity);
                            Log.i("GET RESPONSE—-", outPut);

                            //is = entity.getContent();
                            Log.e("log_tag ******", "good connection");

                            bitmapOrg.recycle();

                        } catch (Exception e) {
                            Log.e("log_tag ******", "Error in http connection " + e.toString());
                        }
                    }
                    return outPut;
                }      
            }

【问题讨论】:

  • “它不工作” ==> LogCat、stacktrace、预期/实际行为?

标签: android android-fragments actionbarsherlock server


【解决方案1】:

您是否有可能在父活动中覆盖 OnActivityResult 但忘记调用 super.OnActivityResult

这是必要的,因此父母可以将呼叫发送给他的孩子(在这种情况下是您的片段)。

【讨论】:

  • 不明白......请解释......我添加了这个 onActivityResult 方法并从片段中删除其他代码存在但不工作......@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO 自动生成的方法存根 if (resultCode == Activity.RESULT_OK) { if (requestCode == 1) { // currImageURI 是 I,Äôm 用来保存内容的全局变量:currImageURI =数据.getData(); } } super.onActivityResult(requestCode, resultCode, data); }
  • 当您在片段 AND 活动中实现 OnActivityResult 方法时,该活动是接收调用 OnActivityResult 的活动。通过使用super.OnActivityResult,活动可以将调用传递给它的孩子(你的片段)。我建议您使用 Log` 来记录您的方法是否收到调用。
猜你喜欢
  • 2015-05-04
  • 2017-05-04
  • 2012-04-03
  • 2014-09-04
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 2012-01-13
  • 2014-04-19
相关资源
最近更新 更多