简体   繁体   中英

starting activity with a non-activity class

I have an onClick method of a checkbox in a class that extends baseAdapter here:

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

                }

From what I've seen, I would need an intent from this activity to my other class where I get the created bitmap made in the onClick method above. The issue is that my other class also extends baseadapter so I can't use startActivity().

Here is where I get the bitmap from storage (in other class):

@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;
}

How can I make it so that I can send intent to my other class so I can get the bitmap?

ADDED:

Does this work the same way? (Please check my coding)

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

for one class and then

@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;
}

in my adapter with the GVABackup class like this:

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();
    }
}}

I don't fully understand what you are asking but would the LocalBroadcastmanager do what you need?

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM