上传带有进度条的大文件,Android中没有OutOfMemory错误

我在上传大型video文件时遇到了一些问题(高达150MB)

1.当我使用这个代码Link1 。我能够上传带有进度条的小文件,但由于我的文件很大,所以android给我OutOfMemory错误

如果我使用这个代码Link2 。 我能够上传大文件(这真的是一个很好的解决scheme),但不知道如何显示进度(如20%完成或80%完成等),所以请指导我。

Solutions Collecting From Web of "上传带有进度条的大文件,Android中没有OutOfMemory错误"

最后,我得到了我想分享的问题的解决scheme…

1.第一个解决schemeLink1这个解决scheme对于像上传15MB的小文件是可以的,但是如果文件非常大,我不能摆脱OutOfMemory错误。

2.第二个解决scheme( Link2 )确实是一个很好的解决scheme,为了显示进度条,我使用了一个自定义的MultipartEntity类。 代码在这里:

import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; public class CustomMultiPartEntity extends MultipartEntity { private final ProgressListener listener; public CustomMultiPartEntity(final ProgressListener listener) { super(); this.listener = listener; } public CustomMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener) { super(mode); this.listener = listener; } public CustomMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener) { super(mode, boundary, charset); this.listener = listener; } @Override public void writeTo(final OutputStream outstream) throws IOException { super.writeTo(new CountingOutputStream(outstream, this.listener)); } public static interface ProgressListener { void transferred(long num); } public static class CountingOutputStream extends FilterOutputStream { private final ProgressListener listener; private long transferred; public CountingOutputStream(final OutputStream out, final ProgressListener listener) { super(out); this.listener = listener; this.transferred = 0; } public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred); } public void write(int b) throws IOException { out.write(b); this.transferred++; this.listener.transferred(this.transferred); } } } 

而我的活动代码是

 import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import com.example.fileupload.CustomMultiPartEntity.ProgressListener; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private ProgressBar pb; private final String filename = "/mnt/sdcard/vid.mp4"; // private final String filename = "/mnt/sdcard/a.3gp"; private String urlString = "http://10.0.2.2:8080/FileUploadServlet1/UploadServlet"; private TextView tv; long totalSize = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.textView1); findViewById(R.id.button1).setOnClickListener(this); tv.setText("init"); pb = (ProgressBar) findViewById(R.id.progressBar1); } private class Uploadtask extends AsyncTask<Void, Integer, String> { @Override protected void onPreExecute() { pb.setProgress(0); tv.setText("shuru"); super.onPreExecute(); } @Override protected void onProgressUpdate(Integer... progress) { pb.setProgress(progress[0]); } @Override protected String doInBackground(Void... params) { return upload(); } private String upload() { String responseString = "no"; File sourceFile = new File(filename); if (!sourceFile.isFile()) { return "not a file"; } HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(urlString); try { CustomMultiPartEntity entity=new CustomMultiPartEntity(new ProgressListener() { @Override public void transferred(long num) { publishProgress((int) ((num / (float) totalSize) * 100)); } }); entity.addPart("type", new StringBody("video")); entity.addPart("uploadedfile", new FileBody(sourceFile)); totalSize = entity.getContentLength(); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); HttpEntity r_entity = response.getEntity(); responseString = EntityUtils.toString(r_entity); } catch (ClientProtocolException e) { responseString = e.toString(); } catch (IOException e) { responseString = e.toString(); } return responseString; } @Override protected void onPostExecute(String result) { tv.setText(result); super.onPostExecute(result); } } @Override public void onClick(View v) { // TODO Auto-generated method stub new Uploadtask().execute(); } }