Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在片段中上载文件_Android_Android Fragments - Fatal编程技术网

Android 在片段中上载文件

Android 在片段中上载文件,android,android-fragments,Android,Android Fragments,我试图在服务器上上传一个图像,这是我用来从android片段上传文件的代码: class Uploader{ String selectedImagePath; String fileSize; String date; Uploader(String selectedImagePath, String fileSize, String date){ this.selectedImagePath=selectedImagePath;

我试图在服务器上上传一个图像,这是我用来从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.err14489:java.net.SocketException:sendto失败:eppe管道断裂 11-20 12:41:25.949:W/System.err14489:at libcore.io.IoBridge.maybethrowafterendtoiobridge.java:499 11-20 12:41:25.950:W/System.err14489:at libcore.io.IoBridge.sendtoIoBridge.java:468 11-20 12:41:25.950:W/System.err14489:at java.net.PlainSocketImpl.writePlainSocketImpl.java:508 11-20 12:41:25.951:W/System.err14489:at java.net.PlainSocketImpl.access$100PlainSocketImpl.java:46 11-20 12:41:25.951:W/System.err14489:at java.net.PlainSocketImpl$PlainSocketOutputStream.writePlainSocketImpl.java:270 11-20 12:41:25.952:W/System.err14489:org.apache.http.impl.io.AbstractSessionOutputBuffer.WriteAstractSessionOutputBuffer.java:109 11-20 12:41:25.952:W/System.err14489:org.apache.http.impl.io.ContentLengthOutputStream.writeContentLengthOutputStream.java:113 11-20 12:41:25.953:W/System.err14489:org.apache.http.impl.io.ContentLengthOutputStream.writeContentLengthOutputStream.java:119 11-20 12:41:25.953:W/System.err14489:at org.apache.http.entity.mime.content.ByteArrayBody.writeToByteArrayBody.java:83 11-20 12:41:25.954:W/System.err14489:at org.apache.http.entity.mime.HttpMultipart.doWriteToHttpMultipart.java:206 11-20 12:41:25.954:W/System.err14489:at org.apache.http.entity.mime.HttpMultipart.writeToHttpMultipart.java:224 11-20 12:41:25.955:W/System.err14489:at org.apache.http.entity.mime.MultipartEntity.writeToMultipartEntity.java:183 11-20 12:41:25.955:W/System.err14489:at org.apache.http.impl.entity.EntitySerializer.serializeEntitySerializer.java:97 11-20 12:41:25.956:W/System.err14489:at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntityAbstractHttpClientConnection.java:162 11-20 12:41:25.956:W/System.err14489:org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntityAbstractClientConnAdapter.java:272 11-20 12:41:25.957:W/System.err14489:at org.apache.http.protocol.HttpRequestExecutor.doSendRequestHttpRequestExecutor.java:237 11-20 12:41:25.957:W/System.err14489:at org.apache.http.protocol.HttpRequestExecutor.executehttpprequestexecutor.java:119 11-20 12:41:25.964:W/System.err14489:at org.apache.http.impl.client.DefaultRequestDirector.executeDefaultRequestDirector.java:442

当我在活动中运行相同的代码时,它运行良好

我在搜索时发现:

但这也帮不了我


以下是我的完整代码,以防它有助于发现问题:

peer在您向其写入时关闭了连接it@maveňツ 怎么会?我没听懂你的话?我怎么才能不关上它呢?