How to load JPEG as a BITMAP from server using Picasso - android

I've tried using the method:
bmp = (Bitmap) Picasso.with(Feed.this).load(IMAGE_URL+loadImageUrl).resize(width, width).get();
But I get a "Picasso DownloadResponseException" even when I take out the (Bitmap) typeCast
I've also tried:
Picasso.with(Feed.this).load(IMAGE_URL+image).resize(width, width).into(target);
With
private Target target = new Target() {
#Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
Log.d("FAILED", "Bitmap Failed");
}
#Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void onBitmapLoaded(Bitmap bmp, LoadedFrom arg1) {
RoundedCornersDrawable drawable = new RoundedCornersDrawable(getResources(), bmp);
theImage.setImageDrawable(drawable);
}
};
But it gives me nothing, the image doesn't load it just stays blank and the log message under the BitmapFailed method does not come up... The images stored on my server are of type "jpg".
Please help me

Figured it out. Turns out I had the wrong URL. Also make sure you Don't call that Picasso.get() method inside the UI thread...

Related

Sharing Bitmap Image and Text android

I am having a Framelayout and some TextViews in my application for which I am loading data from server and setting the background of FrameLayout with Image loaded from server using Picasso and setting TextViews in the same manner. But I want to share it using intent and I am unable to figure out how to do that? Do I have to download the Image First?
My Code in AsyncTask:
Picasso.with(ctx).load(myPlace.getImg()).into(new Target() {
#Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
// TODO Auto-generated method stub
pImg.setBackgroundDrawable(new BitmapDrawable(ctx.getResources(), bitmap));
}
#Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
Toast.makeText(ctx, "Failed Loading", Toast.LENGTH_SHORT).show();
}
});
pname.setText(myPlace.getName());
pdes.setText(myPlace.getDescription());
Share Button:
Button shareBtn = (Button) findViewById(R.id.sharebtn);
As Picasso will cache the image, you wont need to re download it in another activity(as long as it is on the same application). Add these to your code and see if it helps
steps one and two required only if myPlace is local to AsyncTask
Step 1: make AsyncTask return a MyPlace object
private class ClassName extends AsyncTask<..., ..., MyPlace> {
...
}
Step 2: return myPlace object from onPostExecute
Step 3: pack values in bundle and send over intent
Intent intent = new Intent(this, NextActivity.class)
Bundle bundle = new Bundle();
bundle.putString(IMG_URL, myPlace.getImg()); // assuming getImg returns string of url
// put name and desc in the same way
intent.addExtra(bundle);
startActivity(intent);
Step 4: get values in new activity
Intent intent = getIntent();
String url = intent.getExtra().getString(IMG_URL);
// others in the smae way

IntentService Terminated before Picasso loads Bitmap

I'm trying to use an IntentService for processing and uploading images that's running in a different process to have more memory. I'm Using also Picasso to load the Image. When the Image is small the bitmap is loaded successfully and uploaded, however if the image is big the IntentService is terminated before Picasso is done loading It.
Picasso have to run on UIThread
Here is the code.
private void downloadImage(File file) {
final Uri uri = Uri.fromFile(file);
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
#Override
public void run() {
Picasso.with(NewImageProcessingService.this).load(uri).transform(new ImageLoadingUtil.DecreaseQualityTransformation(imageQuality)).into(NewImageProcessingService.this);
}
});
}
#Override
protected void onHandleIntent(Intent intent) {
File file = (File) intent.getSerializableExtra(KEY_IMAGE_FILE);
imageQuality = ImagesUtils.IMAGE_QUALITY
.values()[intent.getIntExtra(IMAGE_QUALITY, ImagesUtils.IMAGE_QUALITY.DEFAULT.ordinal())];
downloadImage(file);
}
This question is quite old, but if anyone steps by. The Target is getting garbage collected before it can show the bitmap.
Use it like this
public class BitmapLoader {
public static Target getViewTarget(final OnImageLoadingCompleted onCompleted) {
return new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
onCompleted.imageLoadingCompleted(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
}
}
You need to have a strong reference to the Target so have a field in your IntentService holding it e.g.
private Target viewTarget;
viewTarget = BitmapLoader.getViewTarget(bitmap -> {
// do stuff with the bitmap
});
new Handler(Looper.getMainLooper()).post(() -> Picasso.with(getApplicationContext()).load(object.getImageUrl()).into(viewTarget));

How to use same of placeholder as Image size using Picasso

I am trying to put a placeholder of same size as Image to be render inside the ImageView using Picasso. Please help me regarding this.
This is what I have done in my code
Transformation blurTransformation = new Transformation() {
#Override
public String key() {
return "blur()";
}
#Override
public Bitmap transform(Bitmap source) {
// TODO Auto-generated method stub
Bitmap blurred = Blur.fastblur(mContext,
source, 10);
source.recycle();
return blurred;
}
};
Picasso.with(mContext).load(url).placeholder(R.drawable.placeholder_blur).transform(blurTransformation)
.into(Imageview,new Callback() {
#Override
public void onSuccess() {
// TODO Auto-generated method
// stub
Picasso.with(mContext).load(url).placeholder(Imageview.getDrawable()).into(Imageview);
}
#Override
public void onError() {
// TODO Auto-generated method
// stub
}
});
Problem I am facing is I have drawable of size 225 * 225 and for all the size the placeholder image remains same. I want something like facebook having placeholder of same size as the size of image which will load after that.
Use Glide library.
Glide.with(_activity)
.load(url)
.placeholder(R.drawable.ic_launcher).override(200, 200)
.error(R.drawable.ic_launcher).centerCrop()
.into(c_holder.img);

onBitmapLoaded never called

I've got an issue with onBitmapLoaded. The method is not called when it should be (it is called the second time i enter my view). Nevertheless i keep a reference to my target since i add it to an arraylist.
I don't understand why it's not working.
Does someone have an idea ?
public void loadBitmap() {
if(loadtarget == null) {
loadtarget = new Target(){
#Override
public void onPrepareLoad(Drawable arg0) {
Log.d("Bitmap","On prepare load");
targetList.remove(this);
return;
}
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d("Bitmap","OKAY for :" + filename);
targetList.remove(this);
handleLoadedBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.d("Bitmap","Error for :" + filename);
}
};
}
targetList.add(loadtarget);
Picasso.with(context).load(imageUrl).into(loadtarget);
}
If targetList and loadtarget are both local variables then they will be marked for GC collecting as soon as the method finishes.
Make sure targetList is a class variable so that its outlives the method.
I've find some kind of trick to solve my problem.
By replacing :
Picasso.with(context).load(imageUrl).into(targetList.get(i));
With :
Picasso.with(context).load(imageUrl).transform(new Transformation() {
#Override
public Bitmap transform(Bitmap source) {
handleLoadedBitmap(source);
return source;
}
#Override
public String key() {
return "";
}
}).into(imageView); // imageView is a fictive imageView allocated only for this operation
my code is working. I'm not sure that it's the best solution but it fixed my problem.

strange error on my android set frame application

i create an application that set a frame to a image bitmap... but when the process give a result, i don't know, why the result bitmap are also saved to my original bitmap variabel, this is my code :
main code :
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.frame);
tampilFrame = (ImageView)findViewById(R.id.ivframe);
oribmp = temporary.getBitmap();
temp = oribmp;
tampilFrame.setImageBitmap(temp);
frame00 = (ImageView)findViewById(R.id.frs00);
frame00.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr00));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
frame01 = (ImageView)findViewById(R.id.frs01);
frame01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr01));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
frame02 = (ImageView)findViewById(R.id.frs02);
frame02.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr02));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
frame03 = (ImageView)findViewById(R.id.frs03);
frame03.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
frame = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fr03));
frame = Bitmap.createScaledBitmap(frame, 800, 800, false);
new backtask().execute();
}
});
}
}
the variabel frame00, frame01, frame02 are ImageView that I set as a Button. When the user click the frame image, it will load an image from drawable and stored in a variabel named "frame" and also call a asyncTask named "backtask".
this is the "backtask" code
public class backtask extends AsyncTask<Void, Void, Void> {
ProgressDialog prog;
#Override
protected void onPreExecute(){
super.onPreExecute();
prog = ProgressDialog.show(FrameActivity.this, "", "Progress...");
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
temp = setFrame(oribmp);
Log.i("temp", "widht : "+String.valueOf(temp.getWidth())+" Height : "+String.valueOf(temp.getHeight()));
return null;
}
#Override
protected void onPostExecute(Void res){
super.onPostExecute(res);
tampilFrame.setImageBitmap(temp);
prog.dismiss();
Log.i("setFrame", "Complete");
}
}
this backtask code i used to make a loading progress when the application runs the "setFrame()" code. and this is my "setFrame" code :
public Bitmap setFrame(Bitmap bit){
for (int i=0;i<frame.getWidth();i++){
for(int j=0;j<frame.getHeight();j++){
if (Color.alpha(frame.getPixel(i, j))!=0){
bit.setPixel(i, j, frame.getPixel(i, j));
}
}
}
frame.recycle();
return bit;
}
the oribmp is a variabel that containt original bitmap get from class temporary.getBitmap()
when the "temp = setFrame(oribmp);" finish, the ori bmp has also changes same as the temp.
when i checked to class temporary, the bitmap variabel that contain original bitmap on the temporary class also changes to the result of the setFrame...
this is the code pf temporary class
public class temporary {
private static Bitmap Mbmp;
public static Bitmap getBitmap () {
return Mbmp;
}
public static void setBitmap(Bitmap oribimp) {
// TODO Auto-generated method stub
Mbmp = oribimp;
Log.i("editor", "simpen bitmap");
}
}
temporary class is a class that i used to send the bitmap value to another activity in my application.. this is what the FrameActivity looks like on the run..
strart activity
this is the look of FrameActivity when its first start, the original bitmap is that gecko
set frame 1
this is the looks when set the first frame
set frame 2
this is the looks when set the second frame, cause the oribmp has the same value like the result of the first seFrame, on the second setFrame they looks like pile up
im sory if my question is too long, maybe anyone can give me solution :D

Categories

Resources