从intent中设置gridView中的字节数组

时间:2013-11-23 16:40:52

标签: android android-intent android-gridview android-bitmap

我在这里设置了一个复选框的onClick监听器:

@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(), Drag_and_Drop_App.class);
                                intent.putExtra("picture", byteArray);
                                v.getContext().startActivity(intent);
                                Log.d("AppInfoAdapter", "Intent started to send Bitmap");
                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });

我正在尝试将此处发送的Intent(包含位图)发送到我的gridView(这是gridView适配器):

   // Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();

// Constructor
public GridViewAdapter(Context c){
    mContextGV = c;
    Log.d("GridViewAdapter", "Constructor is set");

    drawables.add(R.drawable.pattern1);
    Log.d("GridViewAdapter", "pattern1 added");

    drawables.add(R.drawable.pattern2);
    Log.d("GridViewAdapter", "pattern2 added");

    drawables.add(R.drawable.trashcan);
    Log.d("GridViewAdapter", "trashcan added");

    drawables.add(R.drawable.ic_launcher);
    Log.d("GridViewAdapter", "ic_launcher added");
}

但是由于我没有任何东西可以进入我的适配器,我必须在这里获取位图:(我的gridView实际设置在哪里):

// set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);

    // GridView
    Log.d("D&D", "onCreate called");

    Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("picture");
    Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);

    // Instance of Adapter Class
    gridView.setAdapter(new GridViewAdapter(this));

但是我无法将该位图default_b添加到我的gridView。

我该怎么做?

更新的编码:

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

                                SaveImage(default_b);

                                Intent intent = new Intent(v.getContext(),Drag_and_Drop_App.class);
                                intent.putExtra("picture", fname);
                                v.getContext().startActivity(intent);
                                Log.d("AppInfoAdapter", "Intent started to send Bitmap");

                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }
            });

    // return view
    return v;
}

这是班级:

    public void SaveImage(Bitmap default_b) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 100000;
    n = generator.nextInt(n);
    String fname = "Image-" + n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        default_b.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

您不应该将字节数组传递给intent。 Intent包在活动之间发送时具有最大大小,并且在序列化和反序列化时可能会使应用程序陷入困境。

另请参阅描述您确切问题的this issue,并注意问题已按“按预期工作”结束。用罗曼盖伊的话说:

  

通过Intents传递大量数据非常昂贵。你不应该使用   意图作为位图的传输机制。相反,传递URI或任何其他   位置机制(文件路径等)

我建议您将图像写入缓存目录,并从其他活动中异步读取。