无法将图像发送到服务器(OutOfMemoryError,android)

时间:2015-05-06 07:48:30

标签: java android image server

我无法将图像发送到服务器。我正在尝试这种方式:我单击按钮,选择图像,然后将图像上传到服务器。请给我一个课程或教程如何将图像发送到服务器。也许我做错了什么。低于我的错误:

05-06 10:45:47.578: E/AndroidRuntime(21361): FATAL EXCEPTION: main
05-06 10:45:47.578: E/AndroidRuntime(21361): java.lang.OutOfMemoryError
05-06 10:45:47.578: E/AndroidRuntime(21361):    at java.lang.String.<init>(String.java:432)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:642)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at java.lang.StringBuilder.toString(StringBuilder.java:663)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at libcore.net.UriCodec.encode(UriCodec.java:134)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at java.net.URLEncoder.encode(URLEncoder.java:57)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at org.apache.http.client.utils.URLEncodedUtils.encode(URLEncodedUtils.java:184)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:163)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:71)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at com.example.uploadimagetoserver.MainActivity.onActivityResult(MainActivity.java:96)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.app.Activity.dispatchActivityResult(Activity.java:4663)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3131)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3178)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.app.ActivityThread.access$1100(ActivityThread.java:134)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.os.Looper.loop(Looper.java:154)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at android.app.ActivityThread.main(ActivityThread.java:4624)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at java.lang.reflect.Method.invoke(Method.java:511)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-06 10:45:47.578: E/AndroidRuntime(21361):    at dalvik.system.NativeStart.main(Native Method)

这是我的主要课程:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button1 = (Button) findViewById(R.id.button1);

        //get image path
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                 photoPickerIntent.setType("image/*");
                 final int SELECT_PHOTO = 1234;
                 startActivityForResult(photoPickerIntent, SELECT_PHOTO);
            } 
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        switch(requestCode) { 
        case 1234:
            //if ok upload image
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                String selectedImageName = selectedImage.getLastPathSegment();
                cursor.close();

                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);


                HttpClient httpclient = new DefaultHttpClient();
                //working link
                HttpPost httppost = new HttpPost("http://crm.pavlun.info/api/upload_photo");

                try {
                    if(yourSelectedImage != null) {

                         ByteArrayOutputStream bos = new ByteArrayOutputStream();
                         yourSelectedImage.compress(CompressFormat.JPEG, 100, bos);
                         byte[] data = bos.toByteArray();
                         String file = Base64.encodeToString(data, Base64.DEFAULT);

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

                         nameValuePairs.add(new BasicNameValuePair("image", file));
                         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                         httpclient.execute(httppost);

                         ResponseHandler<String> responseHandler = new BasicResponseHandler();
                         String response = httpclient.execute(httppost, responseHandler);

                         Log.e("response", response);
                    }

                }catch(Exception e) {

                }

            }
        }

1 个答案:

答案 0 :(得分:0)

以下是教程的一些建议......你可以自己找到它们,我仍然觉得可以帮助你。

希望这会对你有所帮助。如果你被困在任何地方,请告诉我。