0

我创建了一个位图,然后将该位图设置为 byteArray

addCheckbox
            .setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        PackageManager pm = mContext.getPackageManager();
                        Drawable icon = null;
                        try {
                            icon = pm
                            .getApplicationIcon(entry.activityInfo.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Drawable default_icon = pm.getDefaultActivityIcon();
                        if (icon instanceof BitmapDrawable
                                && default_icon instanceof BitmapDrawable) {
                            BitmapDrawable icon_bd = (BitmapDrawable) icon;
                            Bitmap icon_b = icon_bd.getBitmap();
                            BitmapDrawable default_bd = (BitmapDrawable) pm
                                    .getDefaultActivityIcon();
                            Bitmap default_b = default_bd.getBitmap();
                            if (icon_b == default_b) {
                                // It's the default icon
                                scaleDownBitmap(default_b, 100, v.getContext());
                                Log.d("AppInfoAdapter", "Scale Bitmap Chosen");

                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                default_b.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                byte[] byteArray = stream.toByteArray();
                                Log.d("AppInfoAdapter", "Scale Bitmap to Array");

                                Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
                                intent.putExtra("picture", byteArray);
                                v.getContext().startActivity(intent);
                                Log.d("AppInfoAdapter", "Intent started to send Bitmap");
                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });

并尝试在我的 GridView 中将其设置为图像:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drag_and_drop_app);
    Log.d("GridView", "onCreate called");
    drawables.add(R.drawable.pattern1);
    drawables.add(R.drawable.pattern2);
    Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("picture");
    Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    view.setImageBitmap(default_b); !THIS LINE IS NOT CORRECT!
    drawables.add(default_b); !THIS LINE IS NOT CORRECT!

然后这是我的 gridView 适配器:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(mContextGV);
    }
    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

当然不是完整的适配器,而只是我获得视图的部分

所以我无法在 gridView 适配器中获取位图,因为 getIntent() 不适用于基本适配器,所以我试图在调用我的适配器的类中获取它。我不知道该怎么做。我已经尝试将它添加到我的 drawables ArrayList 中,然后将其设置为视图,但这不起作用。我将如何获得这个额外的?(((我怎样才能获取数组,然后设置它以使其显示在我的 GridView 中?))

因此,要了解我想要做的事情是从我的适配器获取视图,然后将我在附加组件中获得的位图设置为 setImageBitmap()?我可以这样做(如果没有,怎么做???)

4

1 回答 1

0

将您的 Activity 上下文传递给 Adapter 构造函数

在那里你可以像这样访问你的意图

((Activity)mContext).getIntent()
于 2013-11-14T03:49:03.920 回答