上传片段内的文件

时间:2014-11-20 08:27:33

标签: android android-fragments

我试图在服务器上传图像这里是我用来从我的android片段上传文件的代码:

class Uploader{
    String selectedImagePath;
    String fileSize;
    String date;

    Uploader(String selectedImagePath, String fileSize, String date){
        this.selectedImagePath=selectedImagePath;
        this.fileSize=fileSize;
        this.date=date;
    }

    void upload(){
        SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0); 
        String name = pref.getString("username", null);
        String param2 = name;
        String fileNameArray[] = selectedImagePath.split("/");
        String fileName = fileNameArray[fileNameArray.length-1];
        SendHttpRequestTask t = new SendHttpRequestTask();      
        Toast.makeText(SpaceFragment.this.getActivity(), "CURRENTLY UPLOADING "+selectedImage+","+param2, Toast.LENGTH_LONG).show();
        String[] params = new String[]{url, selectedImagePath, param2};
        t.execute(params);      
    }


    private class SendHttpRequestTask extends AsyncTask<String, Void, String> { 
        ProgressDialog progressDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(SpaceFragment.this.getActivity());
            progressDialog.setMessage("Uploading File. Please wait");
            progressDialog.setCancelable(false);
            progressDialog.show();

        }
        @Override
        protected String doInBackground(String... params) {
            String url = params[0];
            String param1 = params[1];
            String param2 = params[2];
            String fileNameArray[] = param1.split("/");
            String fileName = fileNameArray[fileNameArray.length-1];
            try {
                Bitmap b = BitmapFactory.decodeFile(param1);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                b.compress(CompressFormat.PNG, 0, baos);
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(url);
                MultipartEntity multiPart = new MultipartEntity();
                multiPart.addPart("param1", new StringBody(param1));
                multiPart.addPart("param2", new StringBody(param2));
                //Toast.makeText(getApplicationContext(),"SIZE IS "+ baos.toByteArray().length, Toast.LENGTH_LONG).show();
                multiPart.addPart("file", new ByteArrayBody(baos.toByteArray(), fileName));
                post.setEntity(multiPart);
                client.execute(post);
            }
            catch(Throwable t) {
                t.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String data) {         
            progressDialog.dismiss();   
            //SpaceFragment.this.cursor.requery();
            //SpaceFragment.this.cursorAdapter.notifyDataSetChanged();
            mySQLiteAdapter = new DriveDatabaseAdapter(SpaceFragment.this.getActivity().getApplicationContext());
            mySQLiteAdapter.openToWrite();
            SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0); 
            String name = pref.getString("username", null);
            String fileNameArray[] = selectedImagePath.split("/");
            String fileName = fileNameArray[fileNameArray.length-1];
            mySQLiteAdapter.insertEntry(name,"IMAGE",fileName,fileSize,date);       
        }   
    }
};

但问题是它给出了java SocketException。

这是StackTrace:

  

11-20 12:41:25.944:W / System.err(14489):java.net.SocketException:sendto failed:EPIPE(Broken pipe)   11-20 12:41:25.949:W / System.err(14489):at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499)   11-20 12:41:25.950:W / System.err(14489):at libcore.io.IoBridge.sendto(IoBridge.java:468)   11-20 12:41:25.950:W / System.err(14489):at java.net.PlainSocketImpl.write(PlainSocketImpl.java:508)   11-20 12:41:25.951:W / System.err(14489):at java.net.PlainSocketImpl.access $ 100(PlainSocketImpl.java:46)   11-20 12:41:25.951:W / System.err(14489):at java.net.PlainSocketImpl $ PlainSocketOutputStream.write(PlainSocketImpl.java:270)   11-20 12:41:25.952:W / System.err(14489):at org.apache.http.impl.io.AbstractSessionOutputBuffer.write(AbstractSessionOutputBuffer.java:109)   11-20 12:41:25.952:W / System.err(14489):at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:113)   11-20 12:41:25.953:W / System.err(14489):at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:119)   11-20 12:41:25.953:W / System.err(14489):at org.apache.http.entity.mime.content.ByteArrayBody.writeTo(ByteArrayBody.java:83)   11-20 12:41:25.954:W / System.err(14489):at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206)   11-20 12:41:25.954:W / System.err(14489):at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224)   11-20 12:41:25.955:W / System.err(14489):at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:183)   11-20 12:41:25.955:W / System.err(14489):at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97)   11-20 12:41:25.956:W / System.err(14489):at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162)   11-20 12:41:25.956:W / System.err(14489):at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:272)   11-20 12:41:25.957:​​W / System.err(14489):at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)   11-20 12:41:25.957:​​W / System.err(14489):at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:119)   11-20 12:41:25.964:W / System.err(14489):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:442)

当我在Activity中运行相同的代码时,运行正常。

我在搜索时得到了这个:Android : Socket - java.net.SocketException: sendto failed: EPIPE (Broken pipe)

但它也没有帮助我

以下是我的完整代码,以防有助于找到问题:http://pastebin.com/s50R4daP

0 个答案:

没有答案