1

名前DOB idなどの他のエントリを使用してギャラリーを介してサーバーに画像をアップロードしようとしましたが、PHPサーバーの画像を除くすべてのエントリがアップロードされました。私の間違いがどこにあるのかわかりませんでした。別のコードを提案してください。教えてください。ここに私のコードがあります

          Bitmap bitmapOrg = BitmapFactory.decodeFile(imageselectedPath1);
     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);
            Log.d("width ", "width  " + width);
            Log.d("height ", "height " + height);
            bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400,
                    newheight, true);
            // Here you can define .PNG as well
            bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 99, bao);
            byte[] ba = bao.toByteArray();
            String ba1 = Base64.encodeBytes(ba);
            Log.d("uploading  ", "uploading   " + ba1);

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("user_id", mSignMessg));
            nameValuePairs.add(new BasicNameValuePair("name", mname.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("dob", mbirthday.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("bio", mbio.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("sex", mSexValue));
            nameValuePairs.add(new BasicNameValuePair("profile_status", "0"));
            nameValuePairs.add(new BasicNameValuePair("location", mlocation.getText().toString()));

            nameValuePairs.add(new BasicNameValuePair("image", ba1));
            Log.d("userfile", "userfile " + ba1);

            // nameValuePairs.add(new BasicNameValuePair("image”, ba1));

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://iapptechnologies.com/snapic/profileXml.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                Log.d("nameValuePairs", "nameValuePairs " + nameValuePairs);

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

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

                // is = entity.getContent();
                Log.e("log_tag ******", "good connection");
                System.out.println("gudconection");
                Log.d("god connection ", "gud connection ");

                bitmapOrg.recycle();

            } catch (Exception e) {

                Log.e("logCatch block***",
                        "Error in http connection " + e.toString());
                Log.d("log_catch block ******", "Error in http connection "
                        + e.toString());
            }

              Log.d("image_name","image_name "+image_name); 
4

2 に答える 2

2

次のコードは、最初に画像を解像度で圧縮してから、MultiPartエンティティを設定し、メソッドを介して Web 経由でデータを送信しますPOST

BitmapFactory.Options options = new BitmapFactory.Options();

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = true;
    // image path `String` where your image is located
    BitmapFactory.decodeFile(imagePath, options);

    int bitmapWidth = 400;
    int bitmapHeight = 250;

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > bitmapHeight || width > bitmapWidth) {
        if (width > height) {
            inSampleSize = Math
                    .round((float) height / (float) bitmapHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) bitmapWidth);
        }
    }

    options.inJustDecodeBounds = false;
    options.inSampleSize = inSampleSize;


    // you can change the format of you image compressed for what do you
    // want;
    // now it is set up to 640 x 480;

    Bitmap bmpScale = BitmapFactory.decodeFile(imagePath, options);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    // CompressFormat set up to JPG, you can change to PNG or whatever you
    // want;

    bmpScale.compress(CompressFormat.JPEG, 100, bos);

    byte[] data = bos.toByteArray();

    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);

    entity.addPart("avatar", new ByteArrayBody(data,"pic.jpg"
            ));

    //add your other name value pairs in entity.

    for (int i = 0; i < nameValuePairs.size(); i++) {
        entity.addPart(nameValuePairs.get(i).getName(), new StringBody(
                nameValuePairs.get(i).getValue()));
    }

    httppost.setEntity(entity);

    HttpResponse response = httpClient.execute(httppost);

編集

あ、すみません、お知らせするのを忘れていました。httpmime-4.2.2はい、 andという名前の jar がありhttpclient.jarます。Apache サイトからダウンロードできます。パッケージを解凍するだけで、 のディレクトリが得られますlib

于 2012-12-25T05:49:54.760 に答える
2

http://iapaaaa.com/snapic/profileXml.php?user_id=2&name=joe&dob=2000-01-10&bio=bla bla bla&sex=1&profile_status=0&location=England&userfile= user_photo.png のような ulr の場合

このコードを使用 BitmapFactory.Options options = new BitmapFactory.Options();

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = true;
            // image path `String` where your image is located
            BitmapFactory.decodeFile(selectedPath1, options);

            int bitmapWidth = 400;
            int bitmapHeight = 250;
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
            if (height > bitmapHeight || width > bitmapWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float) height
                            / (float) bitmapHeight);
                } else {
                    inSampleSize = Math.round((float) width
                            / (float) bitmapWidth);
                }
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = inSampleSize;

            Bitmap bmpScale = BitmapFactory.decodeFile(selectedPath1,
                    options);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            // CompressFormat set up to JPG, you can change to PNG or
            // whatever you
            // want;

            bmpScale.compress(CompressFormat.JPEG, 100, bos);
            bmpScale.compress(CompressFormat.JPEG, 100, bos);

            byte[] data = bos.toByteArray();
            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            // entity.addPart("avatar", new ByteArrayBody(data,mSignMessg +
            // "-" + new Random().nextInt(1000) + ".jpg"));

            entity.addPart("userfile", new ByteArrayBody(data, "pic.jpg"));
            // add your other name value pairs in entity.
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs
                    .add(new BasicNameValuePair("user_id", mSignMessg));
            nameValuePairs.add(new BasicNameValuePair("name", mname
                    .getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("dob", mbirthday
                    .getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("bio", mbio.getText()
                    .toString()));
            nameValuePairs.add(new BasicNameValuePair("sex", mSexValue));
            nameValuePairs
                    .add(new BasicNameValuePair("profile_status", "0"));
            nameValuePairs.add(new BasicNameValuePair("location", mlocation
                    .getText().toString()));

            // nameValuePairs.add(new BasicNameValuePair("image", ba1));
            Log.d("userfile", "userfile " + ba1);

            // nameValuePairs.add(new BasicNameValuePair("image”, ba1));

            for (int i = 0; i < nameValuePairs.size(); i++) {

                try {
                    entity.addPart(
                            nameValuePairs.get(i).getName(),
                            new StringBody(nameValuePairs.get(i).getValue()));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    Log.d("respons", "image respons " + e);
                    e.printStackTrace();
                }

            }

            // httppost.setEntity(entity);
            //
            // HttpResponse response = httpClient.execute(httppost);

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://technologies.com/snapic/profileXml.php");

                httppost.setEntity(entity);

                Log.d("nameValuePairs", "nameValuePairs " + nameValuePairs);

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

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

                // is = entity.getContent();
                Log.e("log_tag ******", "good connection");
                System.out.println("gudconection");
                Log.d("god connection ", "gud connection ");

                bitmapOrg.recycle();

            } catch (Exception e) {

                Log.e("logCatch block***",
                        "Error in http connection " + e.toString());
                Log.d("log_catch block ******", "Error in http connection "
                        + e.toString());
            }
于 2012-12-25T08:05:39.750 に答える