Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用非活动类启动活动_Java_Android_Android Intent_Android Bitmap - Fatal编程技术网

Java 使用非活动类启动活动

Java 使用非活动类启动活动,java,android,android-intent,android-bitmap,Java,Android,Android Intent,Android Bitmap,我在扩展baseAdapter的类中有一个复选框的onClick方法: @Override public void onClick(View v) { if (addCheckbox.isChecked()) { System.out.println("Checked"); PackageManager pm = mContex

我在扩展baseAdapter的类中有一个复选框的onClick方法:

@Override
                public void onClick(View v) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");

                        PackageManager pm = mContext.getPackageManager();
                        final int   DEST_IMAGE_WIDTH = 100;
                        final int DEST_IMAGE_HEIGHT = 100;
                        ApplicationInfo appInfo = mContext.getApplicationInfo();
                        Drawable appIcon = pm.getApplicationIcon(appInfo);
                        Bitmap appBmp  = Bitmap.createBitmap(DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT, Config.ARGB_8888); 

                        // Creates a new canvas based on the image specification
                        // created just above.
                        Canvas canvas = new Canvas(appBmp);
                        // (optional) Fills the entire canvas
                        canvas.drawColor(Color.WHITE);
                        // You need to set bounds otherwise a 0,0 sized image would be drawn.
                        appIcon.setBounds(0, 0, DEST_IMAGE_WIDTH, DEST_IMAGE_HEIGHT);
                        appIcon.draw(canvas);

                        /// Let's save to a .jpg file ...
                        File file = new File(mContext.getFilesDir().getAbsolutePath() + "/test2.jpg");
                        FileOutputStream out;
                        try
                        {
                            file.createNewFile();
                            out = mContext.getApplicationContext().openFileOutput("BitmapImage", Context.MODE_PRIVATE);
                            appBmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
                            Log.i("AppInfoAdapter", "the icon(s) have been saved");
                            out.close();

                            // Load back the image file to confirm it works
                            // Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath() );
                            // ImageView imageV = (ImageView)findViewById(R.id.);
                            // imageV.setImageBitmap(bitmap);
                        }
                        catch (FileNotFoundException e1)
                        {
                            e1.printStackTrace();
                        }
                        catch (IOException e2)
                        {
                            e2.printStackTrace();
                        }

                        // Intent intent = new Intent (v.getContext(), Drag_and_Drop_App.class);
                       // v.getContext().startActivity(intent);
                       // Log.i("AppInfoAdapter", "New intent started to send icon bitmap");

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

                }
从我所看到的情况来看,我需要从这个活动到我的另一个类的意图,在这个类中,我得到了在上面的onClick方法中创建的位图。问题是我的另一个类也扩展了baseadapter,所以我不能使用startActivity

下面是我从其他类的存储中获取位图的位置:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
    }
    if(checked == true){
        isSdReadable();
        try {
            Log.i("GridViewAdapter", "checkbox is checked");
            FileInputStream in = Context.openFileInput("BitmapImage");
            // Load back the image file to confirm it works
            Bitmap bitmap = BitmapFactory.decodeStream(in);
            view.setImageBitmap(bitmap);
            in.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // getThumbnail();
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        Log.e("GridView", "Icons not for use/checkbox not checked");
    }
    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;
}
我如何制作它,以便将意图发送到我的其他类,以便获取位图

增加:

这也一样吗?请检查我的编码

Intent intent = new Intent (v.getContext(), GVABackup.class);
                       v.getContext().startActivity(intent);
                       Log.i("AppInfoAdapter", "New intent started to send icon bitmap");
一节课,然后

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
    }
    if(checked == true){
        isSdReadable();
        Intent intent = new Intent (view.getContext(), GVABackup.class);
           view.getContext().startActivity(intent);
    } else {
        Log.e("GridView", "Icons not for use/checkbox not checked");
    }
    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;
}
在我的带有GVABackup类的适配器中,如下所示:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class GVABackup extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    ImageView view = (ImageView)findViewById(R.id.layout1);

    Log.i("GridViewAdapter", "checkbox is checked");
    FileInputStream in = null;
    try {
        in = openFileInput("BitmapImage");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Load back the image file to confirm it works
    Bitmap bitmap = BitmapFactory.decodeStream(in);
    view.setImageBitmap(bitmap);
    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}

我不完全理解您的要求,但LocalBroadcastmanager会满足您的要求吗


我已经把你的问题读了4遍,弄不明白你在问什么。在一句话中,您说您的类扩展了BaseAdapter。在另一个例子中,你说这是一个活动。好吧,环顾四周后,我注意到在通过FileInput/Output stream向活动发送位图时,我需要使用意图。我发送到的第二个活动没有扩展活动,因此我不能在该类的意图中使用startActivity,否则我会出错。我该怎么做才能使用某种意图?如果你想发送意图,你需要将一个类作为一个活动。这是唯一可能的方法嗯,好的,那么我可以在我的ifchecked==true部分取出try{}语句的一部分,并将其放入另一个扩展活动的类中,然后将意图发送给该类吗?@user251182要了解我的意思,请查看我问题的更新部分,并告诉我这是否有效