1

I want to know how to use AsyncTask or anything eles that will help to load a lot of images (nearly 30 images) in one layout. because the app won't open it always triggers an ANR

here is my xml but does not include all images:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="150dp" android:layout_height="90dp" android:src="@drawable/hk2fm" /> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#000000" /> <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="10dip" android:text="Weapons" android:textSize="20sp" android:textStyle="bold" /> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="136dip" android:layout_gravity="center" > <LinearLayout android:layout_width="150dip" android:layout_height="156dip" android:layout_gravity="center" android:orientation="horizontal" > <ImageButton android:id="@+id/staff" android:layout_width="90dip" android:layout_height="90dip" android:layout_gravity="center" android:scaleType="centerCrop" android:src="@drawable/magestaff" android:background="@android:color/transparent" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> <ImageButton android:id="@+id/images1" android:layout_width="140dip" android:layout_height="140dip" android:layout_gravity="center" android:scaleType="centerCrop" android:src="@drawable/obok" android:background="@android:color/transparent" /> <ImageButton android:id="@+id/shield" android:layout_width="90dip" android:layout_height="90dip" android:layout_gravity="center" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/knight_shield" /> </LinearLayout> </HorizontalScrollView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enemies" android:layout_gravity="center" android:textSize="20sp" android:textStyle="bold" android:paddingTop="10dp" /> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="136dip" android:layout_gravity="center" > <LinearLayout android:layout_width="150dip" android:layout_height="150dip" android:layout_gravity="center" android:orientation="horizontal" > <ImageButton android:id="@+id/heart" android:layout_width="90dip" android:layout_height="90dip" android:layout_gravity="center" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/heart" /> <ImageButton android:id="@+id/xiii" android:layout_width="140dip" android:layout_height="140dip" android:layout_gravity="center" android:scaleType="centerCrop" android:src="@drawable/xiii2" android:background="@android:color/transparent" /> <ImageButton android:id="@+id/imagesn" android:layout_width="90dip" android:layout_height="90dip" android:layout_gravity="center" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/imagesn" /> </LinearLayout> </HorizontalScrollView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Accessories" android:layout_gravity="center" android:textSize="20sp" android:textStyle="bold" android:paddingTop="10dip" /> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="136dip" android:layout_gravity="center" > <LinearLayout android:layout_width="150dip" android:layout_height="150dip" android:layout_gravity="center" android:orientation="horizontal" > <ImageButton android:id="@+id/armor" android:layout_width="90dip" android:layout_height="90dip" android:layout_gravity="center" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/buster_band" /> <ImageButton android:id="@+id/xiii2" android:layout_width="140dip" android:layout_height="140dip" android:layout_gravity="center" android:scaleType="centerCrop" android:src="@drawable/xiii2" android:background="@android:color/transparent" /> <ImageButton android:id="@+id/accessories" android:layout_width="90dip" android:layout_height="90dip" android:layout_gravity="center" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/cosmic_arts" /> </LinearLayout> </HorizontalScrollView> </LinearLayout> </ScrollView> </LinearLayout> 

and this is my java:

import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.media.MediaPlayer; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.ImageButton; public class hk2fm extends Activity { private ImageButton imgButton; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.hk2fm2); final MediaPlayer buttonSound = MediaPlayer.create(hk2fm.this, R.raw.btnclick); ImageButton staff = (ImageButton) findViewById(R.id.staff); ImageButton accessories = (ImageButton) findViewById(R.id.accessories); ImageButton armor = (ImageButton) findViewById(R.id.armor); ImageButton heart = (ImageButton) findViewById(R.id.heart); ImageButton images1 = (ImageButton) findViewById(R.id.images1); ImageButton imagesn = (ImageButton) findViewById(R.id.imagesn); ImageButton shield = (ImageButton) findViewById(R.id.shield); ImageButton xiii = (ImageButton) findViewById(R.id.xiii); staff.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); accessories.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); armor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); heart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); images1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); imagesn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); shield.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); xiii.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub buttonSound.start(); v.setBackgroundColor(Color.parseColor("#FFCC33")); startActivity(new Intent("com.example.hkguide.INFO")); } }); } } 
1
  • My tryings with AsyncTask were almost always painful too. Learning here with your thoughts. Thank you. Commented Feb 18, 2018 at 1:13

2 Answers 2

1

Create a class that extends AsyncTask and put your async method in doInBackground(...) like this:

private classImageUpload extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { //your upload code here return "Executed"; } @Override protected void onPostExecute(String result) {} @Override protected void onPreExecute() {} @Override protected void onProgressUpdate(Void... values) {} 

} Then call your asyncTask in oncreate method like this:

new ImageUpload().execute(); 
Sign up to request clarification or add additional context in comments.

Comments

1

I really recommend you to check Picasso It's a image library, pretty well implemented to performance and memory management. Oh, and also a piece of cake to use and learn !

Simply implementing an AsyncTask to load will do a small part of the job, like memory management, resizing, cropping and another image related stuff, which may appear in the future !

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.