允许用户更改图标

时间:2013-11-17 05:31:35

标签: android android-imageview android-icons

我有一个imageView,它是为我的图标设置的,如下所示:

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

我正在尝试允许用户更改它。我允许他们在我制作的名为Personalize的类中执行此操作,并让他们能够从他们的画廊中选择一个图像,然后在他们点击一个名为“Set Image”的按钮之前在预览中显示该图像(另一个imageView)然后他们选择的图片被发送到活动以设置如下图标:

private void setIconImageInWidget() {
// TODO Auto-generated method stub
Log.d("Personalize", "setIconImageInWidget() called");
Intent i = getIntent();
//Convert bitmap to byte array to send back to activity
// See: http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android
scaleDownBitmapForIcon(b2, 500, this.getBaseContext());
Log.d("Personalize", "Scale Bitmap Chosen For Icon");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b2.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[]byteArray = stream.toByteArray();

i.putExtra("myIconBitmap", byteArray);
setResult(RESULT_ICON, i);
finish();
} 

然后在此处设置:

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

((setBackgroundImageForIcon class(UPDATED):

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

))

这没有错误并返回正确的请求代码,但它似乎根本不会更改图标。我也做了这样,当用户退出程序时,它保存图像路径,然后在onResume()方法上重新加载它。

所以我的问题是图标没有设置为用户选择的图像。为什么是这样?

如果我以错误的方式处理此问题或需要实施更多代码,请告知我们。

增加:

当我写这个问题时,我可能真的应该添加这个但是...... 众所周知,这被设置为一个小部件,所以这里也是我的AppWidgetProvider:

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

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

1 个答案:

答案 0 :(得分:0)

我之前发过一个答案。它从相机意图中拍摄照片(Youcould提供图库图像),然后在保存之前显示它,然后你有两个名为DONE和CANCEL的文本视图,在他们的听众中你可以做任何你想做的事。

Android camera to take multiple photos