Step1. The LoadImageTask class under the com.example.task.LoadImageTask.java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.net.URL; /** * Created by amit rawat and email:amitrawatamit8@gmail.com. */ public class LoadImageTask extends AsyncTask<String, Void, Bitmap> { private Listener mListener; public LoadImageTask(Listener listener) { mListener = listener; } @Override protected Bitmap doInBackground(String... args) { String arg = args[0]; Log.e("arg", "" + arg); try { return BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent()); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Bitmap bitmap) { if (bitmap != null) { mListener.onImageLoaded(bitmap); } else { mListener.onError(); } } public interface Listener { void onImageLoaded(Bitmap bitmap); void onError(); } } |
Step2. The MainActivity.java
import com.example.task.LoadImageTask;
/** * Created by amit rawat and email:amitrawatamit8@gmail.com. */ public class MainActivity extends AppCompatActivity implements LoadImageTask.Listener { ImageView Vendor_Login_image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.vendor_activity_navigationdrawer); Vendor_Login_image = (ImageView) findViewById(R.id.Vendor_login_image); new LoadImageTask(MainNaviScreen.this).execute("image url path");// } @Override public void onImageLoaded(Bitmap bitmap) { Vendor_Login_image.setImageBitmap(bitmap);//if image found } @Override public void onError() { Glide.with(getApplicationContext()).load(R.drawable.ab_userwhite).crossFade(1000) .into(Vendor_Login_image);//no image found and r.drawable.ab_userwhite is the default image } } |
No comments:
Post a Comment