I'm getting my image through an URL and trying to set that to my imageView. But I'm getting a Null Pointer Exception. It is not even entering my asynchtask.
Is there something I'm doing wrong or not seeing?
public class DetailsActivity extends Activity {
ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
final Bundle extras = getIntent().getExtras();
String img = extras.getString("Thumbnail");
new DownloadImageTask((ImageView) findViewById(R.id.btnThumbnail))
.execute("http://mysite.com/images/"
+ img);
}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
public DownloadImageTask(ImageView bmImage) {
thumbnail = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Log.e("URL",urldisplay);
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
thumbnail.setImageBitmap(result);
}
}
}
ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);
You need to get your image after inflating the layout, otherwise findViewById will returns null :
ImageView thumbnail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
thumbnail = (ImageView) findViewById(R.id.btnThumbnail)
Related
I am trying to set my ImageView.I have the url of the image and trying to make it bitmap and after set this bitmap to my ImageView.However, Bitmap result, argument of onPostExecute, is coming as null from download_Image function. It means ' BitmapFactory.decodeStream(is); ' is returning null.
this.ImageView1 = (ImageView) infoWindow.findViewById(R.id.ImageView1);
ImageView1.setTag(URL);
new AsyncTask<ImageView, Void, Bitmap>(){
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
final Bitmap[] b = new Bitmap[1];
this.imageView = imageViews[0];
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
b[0] =download_Image((String) imageView.getTag());
}
});
return b[0];
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
public Bitmap download_Image(String url) {
//---------------------------------------------------
URL newurl = null;
try {
newurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap mIcon_val = null;
try {
URLConnection urlConnection=newurl.openConnection();
InputStream is=urlConnection.getInputStream();
mIcon_val=BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon_val;
//---------------------------------------------------
}
}.execute(ImageView1);
How can I deal with that problem?
Use this code and comment if any problem`public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private String imageUrl ="image url";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageview);
new AsyncTask<ImageView, Void, Bitmap>(){
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
final Bitmap[] b = new Bitmap[1];
this.imageView = imageViews[0];
b[0] =download_Image(imageUrl);
return b[0];
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
public Bitmap download_Image(String url) {
//---------------------------------------------------
URL newurl = null;
try {
newurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap mIcon_val = null;
try {
URLConnection urlConnection=newurl.openConnection();
InputStream is=urlConnection.getInputStream();
mIcon_val= BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon_val;
//---------------------------------------------------
}
}.execute(imageView);}}`
Write this in your gradle dependencies
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
In your activtiy
Glide.with(activity.this).load(your image url).into(imageView);
I am trying to retrieve the Facebook Image from profilepictureview and convert it to bitmap but it gives me a blank profile pic. My main goal is to convert the image view to CircularImageView.
https://github.com/Pkmmte/CircularImageView
The code is set in the onCreate method:
profilePictureView = (ProfilePictureView) findViewById(R.id.image_mainlobby);
profilePictureView.setProfileId(userid);
profilePictureView.setPresetSize(ProfilePictureView.LARGE);
ImageView fbImage = ( ( ImageView)profilePictureView.getChildAt( 0));
Bitmap bitmap = ( ( BitmapDrawable) fbImage.getDrawable()).getBitmap();
ImageView im = (ImageView)findViewById(R.id.fb_profile);
im.setImageBitmap(bitmap);
1)onCreate method:
new DownloadImageTask((CircularImageView) findViewById(R.id.fb_cir))
.execute("https://graph.facebook.com/"+userid+"/picture?type=large");
2) create a new class
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I want to take the image from a URL and set is as the background of my RelativeLayout. I use the AsyncTask and try to create a Drawable from BitmapDrawable. But I get a NullPointerException and can't seem to figure out why?
class DownloadBackgroundTask extends AsyncTask<String, Void, Bitmap> {
RelativeLayout bmImage;
View vi;
public DownloadBackgroundTask(RelativeLayout bmImage, View vi) {
this.bmImage = bmImage;
this.vi = vi;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if (result != null)
{
// null pointer exception here
Drawable d = new BitmapDrawable(vi.getContext().getResources(), result);
bmImage.setBackground(d);
}
else
{
bmImage.setBackgroundResource(R.drawable.logo);
}
}
}
onCreateView():
RelativeLayout rl = (RelativeLayout) rootView.findViewById(R.id.RelativeLayout1);
new DownloadBackgroundTask(rl, rootView).execute("http://pctechmag.com/wp-content/uploads/2012/10/fifa13_messi.jpg");
I have a ByteArray File on my server which I want to read and display in a ImageView.
How can I do that?
convert your byteArray to a bitmap then set the ImageView's bitmap.
Bitmap bm = BitmapFactory.decodeByteArray(myByteArray, 0, byteArray.length);//myByteArray is the byte array you want to convert.
ImageView myImage = (ImageView)findViewById(R.id.custom_image);//get a reference to your ImageView
myImage.setImageBitmap(bm)//set the ImageView bitmap
I found my solution, here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// show The Image
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("http://myURL");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I'm trying to display an image from a url in a "InfoWindowAdapter"
,I have the following code, but does not show me the image
....
mMap = getMap();
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
...
#Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(
R.layout.info_window_layout, null);
String image_url = "http://api.androidhive.info/images/sample.jpg";
new DownloadImageTask(imgEquipo).execute(image_url);
return v;
}
});
call to AsyncTask to get image
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
} }
}
tranks for your help.
It won't work because the view is returned before you get the image, and they are not synchronized.
Take a look at this approach:
I my using google mapV2 and i m downloading image from google place api and want to display in the popup
The trick is to update the InfoWindow after you get the image.