0

I am allowing the user to change the icon in the class Personalize by sending a request code holding an image from a user gallery. The setIconImageinWidget() method sends the result here (in Drag_and_Drop_App):

     else if(requestCode == RESULT_ICON){
         byte[] byteArray = data.getByteArrayExtra("myIconBitmap"); 
         Bitmap myIcon = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
         setBackgroundImageForIcon(myIcon); 
         Log.d("Drag_and_Drop_App", "Icon is set");
     }
     } 

Here is the setBackgroundImageForIcon method:

         @SuppressLint("NewApi") 
     private void setBackgroundImageForIcon(Bitmap bitmap) { 
     ImageView ivICON = (ImageView) findViewById(R.id.bwidgetOpen);

     Drawable dq = new BitmapDrawable(getResources(), bitmap); 

     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
     ivICON.setImageDrawable(dq);
     } else { 
     ivICON.setImageDrawable(dq);
     Log.d("Drag_and_Drop_App", "Icon is set");
     } 
     } 

This returns no errors but the icon is not changed at all based on whatever picture the user chooses to use.

After looking around a while I realized that I would have to change the app widget provider section of my coding here:

package com.example.awesomefilebuilderwidget;
IMPORTS
public class AFBWidget extends AppWidgetProvider{

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    Random r = new Random();
    int randomInt = r.nextInt(1000000000);
    String rand = String.valueOf(randomInt);

    final int N = appWidgetIds.length;

    for (int i = 0; i < N; i++){
        int awID = appWidgetIds[i];
        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
        v.setTextViewText(R.id.tvwidgetUpdate, rand);
           Intent configIntent = new Intent(context, Drag_and_Drop_App.class);

            PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            v.setOnClickPendingIntent(R.id.bwidgetOpen, configPendingIntent);
           //me trying to set the Bitmap from the above classes somehow... v.setImageViewBitmap(R.id.bwidgetOpen, R.id.);

        appWidgetManager.updateAppWidget(awID, v);
    }

}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onDeleted(context, appWidgetIds);
    Toast.makeText(context, "Thanks for checking us out!", Toast.LENGTH_SHORT).show();
}
}

And the imageView I am changing is this:

<ImageView
    android:id="@+id/bwidgetOpen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:contentDescription="@string/desc"/>

in Widget.xml

How can I change my Widget Provider so that it will allow the changing of the icon? I know this is a lot to read but any help is apperciated!

UPDATED:

         @SuppressLint("NewApi") 
     private void setBackgroundImageForIcon(Bitmap bitmap) { 
         Log.d("Drag_and_Drop_App", "Icon...");
     ImageView ivICON = (ImageView) findViewById(R.id.bwidgetOpen);

     BitmapDrawable dq = new BitmapDrawable(getResources(), bitmap); 

     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
     // ivICON.setImageDrawable(dq);
         ivICON.setImageResource(R.drawable.pattern1);
     } else { 
    // ivICON.setImageDrawable(dq);
     ivICON.setImageResource(R.drawable.pattern1);
     Log.d("Drag_and_Drop_App", "Icon is set");
     } 
     } 
user2909006
  • 253
  • 6
  • 22
  • Can you please limit your code pastings to the relevant stuff? I don't want to read all of that to see how you are changing an icon. – Rick Falck Nov 22 '13 at 23:55
  • I cleaned it up a bit. Hopefully that helps – user2909006 Nov 22 '13 at 23:58
  • That's better. Since it's already a bitmap, just use ivICON.setImageBitmap(bitmap); But I can't see why your code doesn't work. – Rick Falck Nov 23 '13 at 00:13
  • would I use .setImageBitmap(bitmap) literally or is just keeping it the way it is now fine? Yeah me too...would exiting the application so that I could make sure that the icon was changed make any difference? – user2909006 Nov 23 '13 at 00:22
  • I do a lot of imageView.setImageResource(R.id.xxxx) instructions in my app and it always displays right away. It could be your findViewById() is not using the right view? – Rick Falck Nov 23 '13 at 00:27
  • Hmm maybe I should try with the imageView.setImageResource to see if it is even setting up an image? And I don't think so, the bwidgetOpen is the right view for the icon...I'll let you know how it goes with the setImageResource – user2909006 Nov 23 '13 at 00:30
  • That's weird...so maybe I'm just tired and correct me if I am wrong but when I try the line `ivICON.setImageResource(R.id.pattern1);` I get an error on pattern1. pattern1 is an image resource in my drawable folder...I tried other options for setImagexxx and it still gave me an error. – user2909006 Nov 23 '13 at 00:41
  • Ok yeah that was my bad sorry. So I fixed the above mistake and when I ran it, the request code returned back fine, however, the `Log.d("Drag_and_Drop_App", "Icon is set");` line was never logged and it doesn't appear as though the icon changed in any way...please check my updated class in the question – user2909006 Nov 23 '13 at 03:51
  • Would already having a source in the imageView maybe cause an issue? – user2909006 Nov 23 '13 at 03:54

0 Answers0