gpt4 book ai didi

android - onProgressUpdate 上的 ArrayIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-11-30 01:42:11 26 4
gpt4 key购买 nike

我在函数 onProgressUpdate 上出现“ArrayIndexOutOfBoundsException”错误我认为一切正常,但找不到问题所在。当这个任务要运行时,logcat 说 ArrayIndexOutOfBoundsException Lenght=0 index=0 !!!我在这样的链接中看到了这段代码:

How to implement file upload progress bar in android

Upload large file with progress bar and without OutOfMemory Error in Android

我首先测试了上传任务,它工作正常......但是当我添加进度条时它崩溃了......请帮我解决

public class InsertFile extends AsyncTask<String, Integer, String> {

final String name;
final Context parent;
String sourceFileUri;
String upLoadServerUri;
int allByte,perBytes=0;
SeekArc skarc;
private ProgressDialog dialog;

public InsertFile(Context c,String uri,String name,String folder2Up, SeekArc s){
parent = c;
sourceFileUri=uri;
this.name=name;
upLoadServerUri="*******************************";
//skarc=s;
dialog = new ProgressDialog(parent);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Uploading photo, please wait.");
dialog.setMax(100);
dialog.setCancelable(true);
}


@Override
protected String doInBackground(String... params) {

try {

HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
publishProgress();
if (sourceFile.isFile()) {

try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);

// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE",
"multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("bill", sourceFileUri);

dos = new DataOutputStream(conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
+ name + "\"" + lineEnd);

dos.writeBytes(lineEnd);

// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
allByte=bytesAvailable;
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

bytesRead = fileInputStream.read(buffer, 0, bufferSize);
perBytes=0;
perBytes=((allByte-bytesAvailable)*100)/allByte;

while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
perBytes=((allByte-bytesAvailable)*100)/allByte;
publishProgress(Integer.valueOf(perBytes));
}

// send multipart form data necesssary after file
// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
//serverResponseCode = conn.getResponseCode();
//String serverResponseMessage = conn
// .getResponseMessage();

//if (serverResponseCode == 200) {

// messageText.setText(msg);
//Toast.makeText(ctx, "File Upload Complete.",
// Toast.LENGTH_SHORT).show();

// recursiveDelete(mDirectory1);

//}

int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();

Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);

if (serverResponseCode == 200) {
Handler handler = new Handler(parent.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(parent, "File is uploaded", Toast.LENGTH_LONG).show();
}
});
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();


}catch(Exception e){

e.printStackTrace();

}
}
else{
Handler handler = new Handler(parent.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(parent, "No file", Toast.LENGTH_LONG).show();
}
});
}

}

catch (final Exception ex) {

ex.printStackTrace();
Handler handler = new Handler(parent.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast.makeText(parent, ex.toString(), Toast.LENGTH_LONG).show();
}
});
}

return "Executed";
}

@Override
protected void onProgressUpdate(Integer... values) {
dialog.setProgress(values[0]);
}

@Override
protected void onPostExecute(String result) {
dialog.dismiss();
}

@Override
protected void onPreExecute() {
dialog.show();
}

最佳答案

似乎您是在不带任何参数的情况下发布您的进度,因此您的 onProgressUpdate() 值数组为空。

关于android - onProgressUpdate 上的 ArrayIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34343598/

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