Confusion about asynchronous image loading

I am trying to get an image from a raw folder using the AsyncTask class. I have been working on this sample for several hours .. I am not getting any exceptions, but nothing will be displayed on the screen. Probably a lot of things were missed. Could you check my code. What am I doing wrong again? //

public class MainActivity extends Activity {

    ImageView img;
    asynclass Myclass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img=new ImageView(this);
    /***
     *  Field[] filelds=R.raw.class.getFields();
        String [] names=new String[filelds.length];
    for (int i = 0; i < filelds.length; i++) {
       names[i]=filelds[i].getName()+".jpg";

        }
     * 
     * 
     * 
     * */

        //Toast.makeText(MainActivity.this, names[0], 0).show();
        String s="android.resource://"+getApplication().getPackageName()+"/raw/d.jpg/";

        this.setContentView(img, 
            new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        Myclass=(asynclass) new asynclass().execute(s);


    }

    public class asynclass extends AsyncTask<String, Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... params) {

            Bitmap bmp=null;

            bmp=BitmapFactory.decodeFile(params[0]);

            return bmp;
        }



        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if(result!=null){
                img.setImageBitmap(result);

            }
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }



    }
+4
source share
3 answers

Okay, so this is the fastest way to fix your specific problem (as with the smaller changes to get a working snippet), but I will keep track of what should be here.

-, , , , , AsyncTask, null. , , , , BitmapFactory.

:

//Remove this line
//String s="android.resource://"+getApplication().getPackageName()+"/raw/d.jpg/";

this.setContentView(img, 
       new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

//Remove the "s" from your execute
Myclass=(asynclass) new asynclass().execute();

AsyncTask:

@Override
protected Bitmap doInBackground(String... params) {

   Bitmap bmp=null;

   //Change this line to use decodeResource instead of decodeFile
   bmp=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.raw.d);

   return bmp;
}

, .

, , setContentView() , , ImageView , .

xml activity_main.xml, , , :

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

onCreate() :

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

    img = (ImageView) findViewById(R.id.image_view);

    Myclass=(asynclass) new asynclass().execute();
}

.

, !

+2

:

1) raw, R.raw.fileName.

2) setContentView()

3) AsyncTask.

, Android.

+4

@Emmanuel . . . OpenSource.

public class MainActivity extends Activity {
    private ImageView mImage;
    private ImageLoader loader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 2) You shouldn't call setContentView() more than once
        setContentView(R.layout.activity_main);

        mImage = new ImageView(this);

        // 3) No need to cast your AsyncTask.
        new ImageLoader().execute(R.raw.d);
    }

    public class ImageLoader extends AsyncTask<Integer, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Integer... params) {
            // 1) You can access resources from the raw folder using R.raw.fileName
            InputStream is = getResources().openRawResource(params[0]);
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            if(result != null){
                mImage.setImageBitmap(result);
            }
        }

    }
}
0

Source: https://habr.com/ru/post/1541142/


All Articles