gpt4 book ai didi

android - 在 Android 中上传带有进度条且没有 OutOfMemory 错误的大文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:52 26 4
gpt4 key购买 nike

我在上传大型视频文件(最大 150MB)时遇到问题

1.当我使用这段代码时Link1 .我可以上传带进度条的小文件,但由于我的文件很大,所以 android 给我 OutOfMemory Error

2.如果我使用这段代码Link2 .我可以上传大文件(这确实是一个很好的解决方案)但不知道如何显示进度(例如完成 20% 或完成 80% 等等)。所以请指导我。

最佳答案

终于我得到了我想分享的问题的解决方案......

1.第一个解决方案 Link1 此解决方案适用于小文件,例如图片上传高达 15MB。但如果文件非常大,我无法摆脱 OutOfMemory 错误。

2.第二种解决方案( Link2 )确实是一个很好的解决方案,为了显示进度条,我使用了自定义的 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);
}
}
}

我的 Activity 代码是

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();
}

}

关于android - 在 Android 中上传带有进度条且没有 OutOfMemory 错误的大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22874735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com