converting ByteArray File from server to a Image in imageView - android

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);
}
}

Related

android download image port 69

there is an IP camera and I want to download a snapshot image.
this is the url : http://test:11223344!!#85.105.152.138:69/ISAPI/Streaming/channels/2/picture
as you can see; port is 69 not 80
this is my code:
ImageView img = findViewById(R.id.imgSnap);
new DownloadImageTask(img).execute("http://test:11223344!!#85.105.152.138:69/ISAPI/Streaming/channels/2/picture");
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 webImage = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
webImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return webImage;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I got FileNotFoundException. but I think the reason is port 69.
How can I get the image?

load .png from url's in the existing imageview

i seen this, and made how they say.
my app loads and shows .jpg, but don't show .png. He shows empty screen. (tested on versions 4.1 and 4.2)
if (isOnline()) {
new DownloadImageTask((ImageView) findViewById(R.id.imageViewSA))
.execute(img);
} else {
tv_info.setText("Включите интернет для загрузки");
}
}
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);
}
you can use universal image loader library instead, so easy to integrate in your code, you can display images from URL to imageView like this
imageLoader.displayImage(url, imageView);
here's a good example of using UIL
and the library
good luck ;)

Android: Convert ProfilePictureView to Bitmap fail Facebook SDK 4.1

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);
}
}

Set the background of RelativeLayout from Url

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");

Null pointer exception when getting image from url to put in imageview

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)

Categories

Resources