1
I’m creating a small gallery and I’m having trouble uploading the images using Asynctask
class Adapter:
public class AdapterGaleriaFragment extends BaseAdapter {
Context ctx;
List<ImageDataModel> lista;
public AdapterGaleriaFragment(Context ctx, List <ImageDataModel> lista){
this.ctx = ctx;
this.lista = lista;
}
@Override
public int getCount() {
return lista != null ? lista.size():0;
}
@Override
public Object getItem(int position) {
return lista.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null) {
view = LayoutInflater.from(ctx).inflate(R.layout.layout_adapter, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder)view.getTag();
}
ImageDataModel img = lista.get(position);
loadBitmap(img.getImagePath(),holder.imgView);
return view;
}
public void loadBitmap(String path, ImageView view) {
Task task = new Task(view);
task.execute(path);
}
}
class of Asynctask:
public class Task extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public Task(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
File file = new File(params[0]);
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap){
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
Log:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #5 Process: com.example.alex_sama.galeriramesmo, PID: 25570 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:304) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.OutOfMemoryError: Failed to allocate a 3686412 byte allocation with 2714916 free bytes and 2MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417) at com.example.alex_sama.galeriramesmo.Task.doInBackground(Task.java:29) at com.example.alex_sama.galeriramesmo.Task.doInBackground(Task.java:19) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
that wouldn’t be it?
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 3686412 byte allocation with 2714916 free bytes and 2MB until OOM at
– Christian Beregula
yes, how can I solve?
– AlexSusama