gpt4 book ai didi

android - 在 Android 中使用线程加载图像时出现内存不足错误。

转载 作者:行者123 更新时间:2023-11-29 02:06:31 24 4
gpt4 key购买 nike

这是我从画廊或相机拍摄照片的代码。我已经实现了一个后台线程来完成任务,然后使用 SmartImageView 使用 url 设置图像。我的错误是内存分配不足。:

   upload.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {





AlertDialog.Builder builder = new AlertDialog.Builder(activity.this);
builder.setMessage("Select") .setCancelable(false).setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent gallIntent=new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
startActivityForResult(gallIntent, 10);
}
})

.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
}
});
AlertDialog alert = builder.create();
alert.show();




if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"Please select image", Toast.LENGTH_SHORT).show();
} else {
dialog = ProgressDialog.show(activity.this, "Uploading",
"Please wait...", true);
//new ImageUploadTask().execute();
}
}
});



}

protected void onActivityResult(int requestCode, int resultCode, final Intent data){


super.onActivityResult(requestCode, resultCode, data);






switch (requestCode) {
case 10:
if (resultCode == Activity.RESULT_OK) {


Thread t = new Thread()
{


public void run(){
Uri imageUri = data.getData();
Bitmap b;
try {
b = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);

String timestamp = Long.toString(System.currentTimeMillis());
MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);
HttpResponse httpResponse;
ByteArrayOutputStream bao = new ByteArrayOutputStream();

b.compress(Bitmap.CompressFormat.JPEG, 100, bao);

byte [] ba = bao.toByteArray();
int f = 0;
String ba1=Base64.encodeToString(ba, f);



try {
OAuth oAuth = new OAuth(activity.this);
HttpPost httpPost = new HttpPost("url");


httpPost.setEntity(new ByteArrayEntity(ba));
HttpClient httpClient= new DefaultHttpClient();
httpResponse = httpClient.execute(httpPost);
int responseCode = httpResponse.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}



}};
t.start();
imgView.setImageUrl(obj.ImageUrl);
}

最佳答案

这个

 String ba1=Base64.encodeToString(ba, f);

很重。我建议使用 http://developer.android.com/reference/android/util/Base64OutputStream.html相反,写入文件,然后在 HttpEntity 中使用 InputStream。

关于android - 在 Android 中使用线程加载图像时出现内存不足错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9767794/

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