发出接收位图并设置为图像

时间:2013-11-14 01:18:08

标签: android gridview bitmap android-gridview

我创建了一个位图,然后将该位图设置为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()?我可以这样做(如果没有,怎么样?)

1 个答案:

答案 0 :(得分:0)

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

你可以像这样访问你的意图

((Activity)mContext).getIntent()