1

I am getting a byteArray from another activity via Intent like this:

        if (view == null) {
        view = new ImageView(mContext);
    }
    Bundle extras = getIntent().getExtras();
      byte[] byteArray = extras.getByteArray("picture");
      Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
      view.setImageBitmap(default_b);
    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;

But I get the error that getIntent() is undefined for the type GridViewAdapter (this is in my base adapter class for a gridView)

I create the intent here:

Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
                                intent.putExtra("picture", byteArray);
                                v.getContext().startActivity(intent);

How can I fix this error?

ADDED:

Here is my full part where I create the intent:

        Log.d("AppInfoAdapter", "Data Set To Display");
    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");
                    }

                }
            });
4

3 回答 3

3

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

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

((Activity)mContext).getIntent()
于 2013-11-14T02:58:48.183 回答
1

getIntent()用于获取Intent用于启动Activity. 由于您不在 an Activitythen 中,因此无法Intent“获取”,并且getIntent()正如它所说,它不是Adapter该类的功能。

Activity在调用该类的代码中使用该代码Adapter并将所需的数据传递给该类

于 2013-11-14T00:55:20.290 回答
1

开始了:

 Intent intent = ((Activity) context).getIntent();
 int value = intent.getIntExtra("myvalue", 0);
于 2017-10-10T12:24:59.763 回答