gpt4 book ai didi

java - Android - AsyncTask 中的 doInBackground() 错误

转载 作者:行者123 更新时间:2023-12-01 18:41:56 24 4
gpt4 key购买 nike

我的应用程序的基本功能是捕获照片或从图库导入,当按下“上传”按钮时,图像将上传到本地主机服务器。

在我将 AsyncTask 实现到流程中之前,上传没有任何问题。现在我已经放入了 AsyncTask,一切都出错了。

我不知道在这个阶段我哪一部分做错了。这是我尝试上传图像文件时 logcat 显示的内容:

10-28 17:23:25.989: E/AndroidRuntime(3356): FATAL EXCEPTION: AsyncTask #5
10-28 17:23:25.989: E/AndroidRuntime(3356): java.lang.RuntimeException: An error occured while executing doInBackground()
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-28 17:23:25.989: E/AndroidRuntime(3356): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-28 17:23:25.989: E/AndroidRuntime(3356): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-28 17:23:25.989: E/AndroidRuntime(3356): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-28 17:23:25.989: E/AndroidRuntime(3356): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-28 17:23:25.989: E/AndroidRuntime(3356): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-28 17:23:25.989: E/AndroidRuntime(3356): at java.lang.Thread.run(Thread.java:856)
10-28 17:23:25.989: E/AndroidRuntime(3356): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.os.Handler.<init>(Handler.java:197)
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.os.Handler.<init>(Handler.java:111)
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.widget.Toast$TN.<init>(Toast.java:324)
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.widget.Toast.<init>(Toast.java:91)
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.widget.Toast.makeText(Toast.java:238)
10-28 17:23:25.989: E/AndroidRuntime(3356): at com.aiman.webshopper.UploadImageActivity$1execMultiPostAsync.doInBackground(UploadImageActivity.java:268)
10-28 17:23:25.989: E/AndroidRuntime(3356): at com.aiman.webshopper.UploadImageActivity$1execMultiPostAsync.doInBackground(UploadImageActivity.java:1)
10-28 17:23:25.989: E/AndroidRuntime(3356): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-28 17:23:25.989: E/AndroidRuntime(3356): at java.util.concurrent.FutureTask.run(FutureTask.java:234)

这是我的上传 Activity 代码:

public class UploadImageActivity extends Activity implements
OnItemSelectedListener {
InputStream inputStream;
private ImageView imageView;

String the_string_response;

private static final int SELECT_PICTURE = 0;
private static final int CAMERA_REQUEST = 1888;

private static final String SERVER_UPLOAD_URI = "...myserver.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_image);

imageView = (ImageView) findViewById(R.id.imgUpload);
}


public void capturePhoto(View view) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(),
"temp.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

public void pickPhoto(View view) {
// TODO: launch the photo picker
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);

imageView.setImageBitmap(bitmap);

String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");

try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK) {
Bitmap bitmap = getPath(data.getData());
imageView.setImageBitmap(bitmap);
}
}

private Bitmap getPath(Uri uri) {

String[] projection = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
int column_index = cursor.getColumnIndexOrThrow(projection[0]);
cursor.moveToFirst();
String filePath = cursor.getString(column_index);
cursor.close();
// Convert file path into bitmap image using below line.
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
return bitmap;
}

public void uploadPhoto(View view) {
try {
executeMultipartPost();
} catch (Exception e) {
e.printStackTrace();
}
}

public void executeMultipartPost() throws Exception {

class execMultiPostAsync extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params){
// Choose image here
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream); // compress to
// which
// format
// you want.
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("image", image_str));

try {
HttpClient httpclient = new DefaultHttpClient();
/*
* HttpPost(parameter): Server URI
*/
HttpPost httppost = new HttpPost(SERVER_UPLOAD_URI);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
the_string_response = convertResponseToString(response);
} catch (Exception e) {
Toast.makeText(UploadImageActivity.this, "ERROR " + e.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println("Error in http connection " + e.toString());
}

return the_string_response;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

Toast.makeText(UploadImageActivity.this,
"Response " + result, Toast.LENGTH_LONG)
.show();

}

public String convertResponseToString(HttpResponse response)
throws IllegalStateException, IOException {

String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); // getting
// content
// lengt
Toast.makeText(UploadImageActivity.this,
"contentLength : " + contentLength, Toast.LENGTH_LONG).show();
if (contentLength < 0) {
} else {
byte[] data = new byte[512];
int len = 0;
try {
while (-1 != (len = inputStream.read(data))) {
buffer.append(new String(data, 0, len)); // converting to
// string and
// appending to
// stringbuffer
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close(); // closing the stream
} catch (IOException e) {
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string

Toast.makeText(UploadImageActivity.this, "Result : " + res,
Toast.LENGTH_LONG).show();
// System.out.println("Response => " +
// EntityUtils.toString(response.getEntity()));
}
return res;
}
}

execMultiPostAsync exec = new execMultiPostAsync();
exec.execute();
}
}

有人可以检查一下我是否正确地将 AsyncTask 任务放入此 Activity 中吗?我想我在某个地方犯了错误。

最佳答案

请从 catch block 中删除 Toast

try {
HttpClient httpclient = new DefaultHttpClient();
/*
* HttpPost(parameter): Server URI
*/
HttpPost httppost = new HttpPost(SERVER_UPLOAD_URI);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
the_string_response = convertResponseToString(response);
} catch (Exception e) {
Toast.makeText(UploadImageActivity.this, "ERROR " + e.getMessage(),
Toast.LENGTH_LONG).show();
System.out.println("Error in http connection " + e.toString());
}

而是使用它来查看 logcat 中的错误,

catch(Exception exception)
{
exception.printStackTrace();
return false;
}

关于java - Android - AsyncTask 中的 doInBackground() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631386/

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