获取内存不足的错误,而使用线程在Android的图像加载。 [英] Getting out of memory error while using threads for Image loading in Android.
本文介绍了获取内存不足的错误,而使用线程在Android的图像加载。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的code采取从画廊或相机拍照。我已经实现了一个后台线程得到完成任务,然后使用SmartImageView使用url设置图像。我的错误是内存分配的:
upload.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(视图v){ AlertDialog.Builder建设者=新AlertDialog.Builder(activity.this);
builder.setMessage(选择).setCancelable(假).setPositiveButton(画廊,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
意图gallIntent =新意图(Intent.ACTION_GET_CONTENT);
gallIntent.setType(图像/ *);
startActivityForResult(gallIntent,10);
}
}) .setNegativeButton(照相机,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
意图cameraIntent =新意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,0);
}
});
AlertDialog警报= builder.create();
alert.show();
如果(位图== NULL){
Toast.makeText(getApplicationContext(),
请选择图片,Toast.LENGTH_SHORT).show();
}其他{
对话框= ProgressDialog.show(activity.this,上传
请稍候...,真正的);
//新ImageUploadTask()执行();
}
}
}); } 保护无效的onActivityResult(INT申请code,INT结果code,最终意向数据){
super.onActivityResult(要求code,结果code,数据);
开关(要求code){
案例10:
如果(结果code == Activity.RESULT_OK){
线程t =新的Thread()
{
公共无效的run(){
乌里imageUri = data.getData();
位图B:
尝试{
B = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri); 串时间戳= Long.toString(System.currentTimeMillis的());
MediaStore.Images.Media.insertImage(getContentResolver(),B,时间戳,时间戳);
HTT presponse HTT presponse;
ByteArrayOutputStream宝=新ByteArrayOutputStream(); b.com preSS(Bitmap.Com pressFormat.JPEG,100,包); 字节[] BA = bao.toByteArray();
INT F = 0;
字符串BA1 = Base64.en codeToString(BA,F); 尝试{
OAuth的OAuth的=新的OAuth(activity.this);
HttpPost httpPost =新HttpPost(URL);
httpPost.setEntity(新ByteArrayEntity(BA));
HttpClient的HttpClient的=新DefaultHttpClient();
HTT presponse = httpClient.execute(httpPost);
。INT响应code = HTT presponse.getStatusLine()的getStatus code();
}赶上(ClientProtocolException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}赶上(FileNotFoundException异常E1){
// TODO自动生成catch块
e1.printStackTrace();
}赶上(IOException异常E1){
// TODO自动生成catch块
e1.printStackTrace();
} }};
t.start();
imgView.setImageUrl(obj.ImageUrl);
}
解决方案
这
字符串BA1 = Base64.en codeToString(BA,F);
很重。我建议使用 http://developer.android.com/reference/android/util /Base64OutputStream.html 相反,写入文件,然后在HttpEntity使用一个InputStream。
This is my code to take a photo from gallery or camera. I've implemented a background thread to get the task done and then using SmartImageView to set the image using the url. My error is Out of Memory Allocation.:
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);
}
解决方案
this
String ba1=Base64.encodeToString(ba, f);
is very heavy. I recommend using a http://developer.android.com/reference/android/util/Base64OutputStream.html instead, write to a file, then use an InputStream in the HttpEntity.
这篇关于获取内存不足的错误,而使用线程在Android的图像加载。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文