无法将图像上传到片段android中的服务器

时间:2014-11-07 11:01:40

标签: android android-fragments actionbarsherlock server

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

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

1 个答案:

答案 0 :(得分:1)

是否有可能覆盖父活动中的OnActivityResult,但忘记拨打super.OnActivityResult

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