Why is the image background purple - android

I'm trying to display this transparent image in a ImageView.
I download and set the image with this class:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private String TAG = "DownloadImageTask";
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.d(TAG, e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Unfortunately the background gets purple. Why does it get purple and how do I solve this?

Not a solution, but more like a fix.
I created a mutable bitmap and changed the colors.
Bitmap copy = result.copy(Bitmap.Config.ARGB_8888, true);
int [] allpixels = new int [ copy.getHeight()*copy.getWidth()];
copy.getPixels(allpixels, 0, copy.getWidth(), 0, 0, copy.getWidth(), copy.getHeight());
for(int i =0; i < copy.getHeight() * copy.getWidth(); i++)
{
if( allpixels[i] == Color.MAGENTA)
{
allpixels[i] = Color.TRANSPARENT;
}
}
copy.setPixels(allpixels, 0, copy.getWidth(), 0, 0, copy.getWidth(), copy.getHeight());
bmImage.setImageBitmap(copy);

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?

download BLOB in AsyncTask

I have a method where I would download an image from a folder based on the link passed into the AsyncMethod
I have since made some changes and now the image resides on the database. I am having a little problem editing my downloadAsyn Task as it no longer receives a link but instead a long string of characters (BLOB from the database).
I have pasted my code below, and is trying to find assistance in assigning cImg1 the bitmap to display my image.
Thank you
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];// this parameter once had url of image
//but now it has the image bitmap.
Bitmap cImg1= null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
// cImg1= BitmapFactory.decodeStream(in);
cImg1=urldisplay;//Assign strings to BitMap?
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return cImg1;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Below code will be worked fine.
public class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> {
Context _context;
ImageView _imageView;
private OnResponseListener _responder;
private String _errorMessage;
public DownloadImageTask(ImageView bmImage, OnResponseListener responseListener) {
this._imageView = bmImage;
_context = bmImage.getContext();
_responder = responseListener;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
int count;
String urlDisplay = urls[0];
Bitmap bitmap = null;
try {
InputStream in = new java.net.URL(urlDisplay).openStream();
BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; options.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(in, null, options);
URLConnection urlConnection = new java.net.URL(urlDisplay).openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
int lengthOfFile = urlConnection.getContentLength();
byte data[] = new byte[1024];
long total = 0;
while ((count = inputStream.read(data)) != -1) {
total += count;
int progress = (int) total * 100 / lengthOfFile;
publishProgress(progress);
}
} catch (Exception e) {
_errorMessage = e.getMessage();
e.printStackTrace();
}
return bitmap;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(Bitmap result) {
if (result != null){
_responder.onSuccess(result);
}
else
_responder.onFailure(_errorMessage);
}
public interface OnResponseListener {
void onSuccess(Bitmap result);
void onFailure(String message);
}
}

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

converting ByteArray File from server to a Image in imageView

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

Categories

Resources