Callback weird behaviour (Android, Picasso library) - java

I am using Picasso library to manage my image uploading and caching. When I am trying to execute this code:
Picasso.with(this)
.load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
.placeholder(R.drawable.calendar)
.error(R.drawable.calendar)
.into(new Target()
{
#Override
public void onPrepareLoad(Drawable drawable)
{
}
#Override
public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
{
cropImage(photo); //not getting here
}
#Override
public void onBitmapFailed(Drawable arg0)
{
}
});
I am not entering int the onBitmapLoaded callback. Only if I close the activity (going back) and re opening it I see the image (entering into onBitmapLoaded).
But if I will change my code by just adding some Toast message into the onPrepareLoad callback every thing works fine. here is the full code:
Picasso.with(this)
.load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
.placeholder(R.drawable.calendar)
.error(R.drawable.calendar)
.into(new Target()
{
#Override
public void onPrepareLoad(Drawable drawable)
{
Toast.makeText(thisActivity, "message", Toast.LENGTH_LONG).show();
}
#Override
public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
{
cropImage(photo);
}
#Override
public void onBitmapFailed(Drawable arg0)
{
}
});
Why the Toast makes it work? what is wrong with it?

I solved the issue by declaring a Target instance as class member. then initialised it. Like this:
target = new Target()
{
#Override
public void onPrepareLoad(Drawable drawable)
{
}
#Override
public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
{
cropEventImage(photo);
}
#Override
public void onBitmapFailed(Drawable arg0)
{
}
};
Picasso.with(this)
.load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
.placeholder(R.drawable.calendar)
.error(R.drawable.calendar)
.into(target);

Related

Getting error while setting speechRecognition.startListenenig() and pass the intent

I get error when I set SPR.startlistenening() method I want to use continuously speech recognition and perform tasks based on results.
I am making an app that countinuously uses speech recognition and do specific task on results:
#Override
protected void onStart() {
super.onStart();
setSPR();
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
SPR.startListening(intent);
}
private void setSPR() {
if (SpeechRecognizer.isRecognitionAvailable(this)){
SpeechRecognizer.createSpeechRecognizer(this);
SPR.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float rmsdB) {
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int error) {
}
#Override
public void onResults(Bundle bundle) {
ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
setRecognitionResults(results.get(0));
}
#Override
public void onPartialResults(Bundle partialResults) {
}
#Override
public void onEvent(int eventType, Bundle params) { }
});
}
}
Error >>>
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.speech.SpeechRecognizer.startListening(android.content.Intent)'
on a null object reference at
com.teamdev.talkingtorch.MainActivity.onStart(MainActivity.java:74) at
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1341)
at android.app.Activity.performStart(Activity.java:7278) at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2937)

UtteranceProgressListener not being Called even after passing hashmap String(read before flagging as duplicate)

I'm trying to put an onUtteranceProgressListener on tts. I've gone through other relevant questions on stackoverflow and followed them. But even after passing utterance id string through Hashmap or Bundle(Since Hashmap is deprecated after api21), utteranceProgressListener isn't getting called. I've used toasts to verify, but there were no toast outputs. I'm getting the audio output, but unable to get the progressListener work on it.
#Override
public void onInit(int initStatus) {
if (initStatus == TextToSpeech.SUCCESS) {
myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
Toast.makeText(InvisibleActivity.this,"listener Started",Toast.LENGTH_SHORT).show();
}
#Override
public void onDone(String utteranceId) {
if (utteranceId.equals("utteranceId")) {
myTTS.stop();
Toast.makeText(InvisibleActivity.this,"mytts stopped",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onError(String utteranceId) {
}
});
myTTS.setLanguage(Locale.US);
} else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry,Text To speech not supported", Toast.LENGTH_SHORT).show();
}
Bundle bundle = new Bundle();
bundle.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"utteranceId");
myTTS.speak(outText, TextToSpeech.QUEUE_FLUSH, bundle,null);
chan();
}
The Hashmap Code (with deprecated speak()) I've used, is:
HashMap<String,String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"utteranceId");
myTTS.speak(outText,TextToSpeech.QUEUE_FLUSH, params);
I've tried both QUEUE_ADD and QUEUE_FLUSH but the result is the same.
This is because it works on another thread.
You need to use the runOnUiThread to make it work:
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String s) {
final String keyword = s;
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Started " + keyword, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onDone(String s) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Done ", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onError(String s) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Error ", Toast.LENGTH_SHORT).show();
}
});
}
});
Make sure that this method is called before the speak method.

Picasso showing blue red and green arrows on top corner

I am using picasso to fire up an image in imageView.
Here is the code
ImageUtils.setImageFromUrl(app.selectedRing.getMainPicture(), imageView, MainActivity.this);
public static void setImageFromUrl(final String url, final ImageView imgView, final Context mContext)
{
Picasso.with(mContext)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.fit().centerInside().placeholder(null)
.into(imgView, new Callback()
{
#Override
public void onSuccess()
{
}
#Override
public void onError()
{
Picasso.with(mContext)
.load(url)
.fit().centerInside()
.into(imgView, new Callback()
{
#Override
public void onSuccess()
{
}
#Override
public void onError()
{
}
});
}
});
}
What i get is
The problem is the blue arrow in left top corner, Sometimes its red/green. I have never seen such a thing before. and its on all the images.
What is going on.
Use picasso.setIndicatorsEnabled(false)
Picasso.with(mContext)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.setIndicatorsEnabled(false)
.fit().centerInside().placeholder(null)
.into(imgView, new Callback()
{
#Override
public void onSuccess()
{
}
#Override
public void onError()
{
Picasso.with(mContext)
.load(url)
.fit().centerInside()
.into(imgView, new Callback()
{
#Override
public void onSuccess()
{
}
#Override
public void onError()
{
}
});
}
});
The color shows the source of image which is being displayed
Red color indicates that image is fetched from network.
Green color indicates that image is fetched from cache memory.
Blue color indicates that image is fetched from disk memory.

how to set background image in Linear layout dynamically using Parser in android

how to set background image in LinearLayout dynamically using Parser in android.
Please help me according the how to set image dynamically.
private void createEventCoverPage() {
final EventCoverData currentEventData = mEventData.get(mCurrentIndex);
LinearLayout mParentLayout = (LinearLayout) findViewById(R.id.eventCoverParentLayoutId);
mParentLayout.setBackground(new BitmapDrawable(getResources(), Picasso.with(getParent()).load(currentEventData.mImageSrc).get()));
}
#Parcel
public static class EventCoverData {
#SerializedName("src")
public String mImageSrc;
}
you can use something like that
Picasso.with(mContext).load(currentEventData.mImageSrc).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mParentLayout.setBackground(new BitmapDrawable(getResources(),bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});

How to listen for Picasso (Android) load complete events?

Is there a way to listen for events from Picasso when using the builder like:
Picasso.with(getContext()).load(url).into(imageView);
I'm trying to call requestLayout() and invalidate() on the parent GridView so it'll resize properly but I don't know how to set a listener or callback.
I see that Picasso has error event reporting, but is there a success event?
You can use a Callback to get onSuccess and onError events. Just add a new Callback to your request like so:
Picasso.with(getContext())
.load(url)
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
}
});
Then you can perform any alterations and modifications in the onSuccess callback.
If you need to access the bitmap before it is loaded to the view, try using :
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed() {
}
}
In the calling method:
Picasso.with(this).load("url").into(target);
Ideally you'd implement Target on a view or view holder object directly.
Hope this helps
Answering #Jas follow up question as a comment to MrEngineer13's answer (since I don't have enough reputation to comment in any answer), you should use the error() method prior to registering the Callback at the into() method, for example:
Picasso.with(getContext())
.load(url)
.error(R.drawable.error_placeholder_image)
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
//Success image already loaded into the view
}
#Override
public void onError() {
//Error placeholder image already loaded into the view, do further handling of this situation here
}
}
);
Square lately has updated Target class and now there are more methods to override (onPrepareLoad and onBitmapFailed):
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
And you still have to use:
Picasso.with(context).load(url).into(target);
private final Callback mImageCallback = new Callback() {
#Override
public void onSuccess() {
startPostponedEnterTransition();
}
#Override
public void onError() {
startPostponedEnterTransition();
}
};
RequestCreator creator = Picasso.with(getActivity()).load(list.get(position).getId());
creator.into(imageView, mImageCallback);
Try This
Picasso.with(context)
.load(services.get(position).getImageInactive())
.into(holder.icon, new Callback() {
#Override
public void onSuccess() {
holder.imageLoad.setVisibility(View.GONE);
}
#Override
public void onError() {
holder.icon.setImageResource(R.drawable.ic_error_image_load);
}
});
As a complement to other answers, in case you don't know where to use original image view, e.g. ImageView myIV:
Original:
Picasso.with(activity).load(url).into(myIV);
New (inside onBitmapLoaded() of new Target()):
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
myIV.setImageBitmap(bitmap);
}

Categories

Resources