在服务器上上传多个图像

时间:2015-04-27 10:07:22

标签: android

彻底解决这个问题我试图在服务器上上传多张图片,它会成功上传一条消息照片,但是当检查服务器时没有图像,我应该怎么做才能帮助那些人....

public ProgressDialog dialog;
public MultipartEntity entity;
public ArrayList<String> map = new ArrayList<String>();
Bundle b;
public String file_path_one;
public String file_path_two;
public String file_path_three;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.upload_images);
    b = getIntent().getExtras();

    if (b != null) {
        ArrayList<String> ImgData = b.getStringArrayList("Images");
        for (int i = 0; i<ImgData.size(); i++) {
            map.add(ImgData.get(i).toString());

        }


    } else {

        Toast.makeText(getApplicationContext(), "No Images Available", Toast.LENGTH_LONG).show();
    }
    System.out.println("Images :" +map.indexOf(0));
}

    public void btnUploadPhotoClick(View v){

        new ImageUploadTask().execute();


    }

    class ImageUploadTask extends AsyncTask<String, Void, String> {

        String sResponse;
         int loadcount = 0;
         File myFile = null;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            dialog = ProgressDialog.show(UploadActivity.this, "Uploading",
                    "Please wait...", true);
            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {


         System.out.println("Images  " + map);


            try {

                String url = "http://transition2uk.com/ios/ios.php";
                //int i = Integer.parseInt(params[0]);
                for(int i=0; i< map.size(); i++){
                     if (i == 0) {
                            file_path_one = map.get(i);
                            myFile = new File(file_path_one);
                        }
                        if (i == 1) {
                            file_path_two = map.get(i);
                            myFile = new File(file_path_two);
                        }
                        if (i == 2) {
                            file_path_three = map.get(i);
                            myFile = new File(file_path_three);
                        }
                        loadcount = i + 1;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.setMessage(" Uploading file " + loadcount
                                        + "/" + map.size());
                            }
                        });


                Bitmap bitmap = decodeFile(map.get(i));         
                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                HttpPost httpPost = new HttpPost(url);

                entity = new MultipartEntity();


                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(CompressFormat.PNG, 10, bos);
                byte[] data = bos.toByteArray();
                String base64 = Base64.encodeBytes(data);

                //entity.addPart("images", new StringBody(base64));
                //entity.addPart("club_id", new StringBody("media[i]"));
                //entity.addPart("media[i]", new StringBody(base64));
                entity.addPart("media[i]", new FileBody(myFile));


                httpPost.setEntity(entity);

                HttpResponse response = httpClient.execute(httpPost);

                sResponse = EntityUtils.toString(response.getEntity());

                System.out.println("sResponse :"+sResponse);


            }
                 /*try {
                        JSONObject jsonObject = new JSONObject(url);
                        String sResponse = jsonObject.getString("sResponse");
                        if (sResponse.equalsIgnoreCase("ok")) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    // new
                                    // MyMediaUploadWebRequestTask(mContext).execute();
                                    doInBackground();
                                }
                            });
                        } else {
                            Toast.makeText(getApplicationContext(), "File uploaded failed!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }*/



                //sResponse = EntityUtils.getContentCharSet(response.getEntity());
                // sResponse = EntityUtils.getContentCharSet(entity);


            } catch (Exception e) {
                if (dialog.isShowing())
                    dialog.dismiss();
                Log.e(e.getClass().getName(), e.getMessage(), e);

            }
            return sResponse;
        }       



        @Override
        protected void onPostExecute(String sResponse) {
            try {
                if (dialog.isShowing())
                    dialog.dismiss();

                if (sResponse != null) {
                    Toast.makeText(getApplicationContext(),
                            sResponse + " Photo uploaded successfully",
                            Toast.LENGTH_SHORT).show();


                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(),
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
            }

        }
    }

    public Bitmap decodeFile(String filePath) {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 1024;
        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
        return bitmap;
    }
}

1 个答案:

答案 0 :(得分:-1)

首先,检查您的方法“decodeFile(String filePath)是否返回任何数据。