开始活动与非活动类 [英] starting activity with a non-activity class

查看:247
本文介绍了开始活动与非活动类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类中的复选框,在这里延伸baseAdapter的一个onClick方法:

  @覆盖
                公共无效的onClick(视图v){
                    如果(addCheckbox.isChecked()){
                        的System.out.println(选中);                        软件包管理系统下午= mContext.getPackageManager();
                        最终诠释DEST_IMAGE_WIDTH = 100;
                        最终诠释DEST_IMAGE_HEIGHT = 100;
                        ApplicationInfo APPINFO = mContext.getApplicationInfo();
                        可绘制APPICON = pm.getApplicationIcon(APPINFO);
                        位图appBmp = Bitmap.createBitmap(DEST_IMAGE_WIDTH,DEST_IMAGE_HEIGHT,Config.ARGB_8888);                        //创建基于图像规范的新画布
                        //创建正上方。
                        帆布帆布=新的Canvas(appBmp);
                        //(可选)填充整个画布
                        canvas.drawColor(Color.WHITE);
                        //你需要设置,否则向那0,0尺寸图像会被绘制。
                        appIcon.setBounds(0,0,DEST_IMAGE_WIDTH,DEST_IMAGE_HEIGHT);
                        appIcon.draw(画布);                        ///让我们保存为.jpg文件...
                        档案文件=新的文件(mContext.getFilesDir()getAbsolutePath()+/test2.jpg);
                        FileOutputStream中出;
                        尝试
                        {
                            file.createNewFile();
                            。OUT = mContext.getApplicationContext()openFileOutput(BitmapImage的Context.MODE_PRIVATE);
                            appBmp.com preSS(Bitmap.Com pressFormat.JPEG,80出);
                            Log.i(AppInfoAdapter,图标(S)已保存);
                            out.close();                            //加载背面的图像文件,以确认它的工作原理
                            //位图位图= BitmapFactory.de codeFILE(file.getAbsolutePath());
                            // ImageView的imageV =(ImageView的)findViewById(R.id);
                            // imageV.setImageBitmap(位图);
                        }
                        赶上(FileNotFoundException异常E1)
                        {
                            e1.printStackTrace();
                        }
                        赶上(IOException异常E2)
                        {
                            e2.printStackTrace();
                        }                        //意向意图=新意图(v.getContext(),Drag_and_Drop_App.class);
                       // v.getContext()startActivity(意向)。
                       // Log.i(AppInfoAdapter,新意图开始发送图标位图);                    }其他{
                        的System.out.println(取消选中);
                    }                }

这是我所看到的,我会在那里我得到了上述的onClick方法取得的创建位图需要从本次活动的意图,我的其他类。问题是,我的其他类也扩展baseadapter所以我不能使用startActivity()。

下面是我得到的存储位图(其他类):

  @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    //尝试重用的意见
    ImageView的观点=(ImageView的)convertView;
    布尔查=(mCheckBox == NULL)假:(((复选框)mCheckBox).isChecked());
    //如果转换视图为null,则创建一个新的实例,否则重用
    如果(查看== NULL){
        鉴于=新ImageView的(上下文);
        Log.d(GridViewAdapter,新ImageView的添加);
    }
    如果(检查==真){
        isSdReadable();
        尝试{
            Log.i(GridViewAdapter,复选框被选中);
            在的FileInputStream = Context.openFileInput(BitmapImage的);
            //加载背面的图像文件,以确认它的工作原理
            位图位图= BitmapFactory.de codeStream(中);
            view.setImageBitmap(位图);
            附寄();
        }赶上(FileNotFoundException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        // getThumbnail();
        赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }其他{
        Log.e(GridView的,没有图标的使用/复选框未选中);
    }
    view.setImageResource(drawables.get(位置));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(新android.widget.GridView.LayoutParams(70,70));
    view.setTag(将String.valueOf(位置));
    返回视图。
}

我怎样才能让这个我可以把意图我的其他类,因此我可以得到位图?

增加:

这是否相同的方式工作? (请查看我的编码)

 意向意图=新意图(v.getContext(),GVABackup.class);
                       。v.getContext()startActivity(意向);
                       Log.i(AppInfoAdapter,新意图开始发送图标位图);

一类,然后

  @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    //尝试重用的意见
    ImageView的观点=(ImageView的)convertView;
    布尔查=(mCheckBox == NULL)假:(((复选框)mCheckBox).isChecked());
    //如果转换视图为null,则创建一个新的实例,否则重用
    如果(查看== NULL){
        鉴于=新ImageView的(上下文);
        Log.d(GridViewAdapter,新ImageView的添加);
    }
    如果(检查==真){
        isSdReadable();
        意向意图=新意图(view.getContext(),GVABackup.class);
           。view.getContext()startActivity(意向);
    }其他{
        Log.e(GridView的,没有图标的使用/复选框未选中);
    }
    view.setImageResource(drawables.get(位置));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(新android.widget.GridView.LayoutParams(70,70));
    view.setTag(将String.valueOf(位置));
    返回视图。
}

在我的适配器像这样的GVABackup类:

 包com.example.awesomefilebuilderwidget;进口公共类GVABackup延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);    ImageView的观点=(ImageView的)findViewById(R.id.layout1);    Log.i(GridViewAdapter,复选框被选中);
    在的FileInputStream = NULL;
    尝试{
        在= openFileInput(BitmapImage的);
    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    //加载背面的图像文件,以确认它的工作原理
    位图位图= BitmapFactory.de codeStream(中);
    view.setImageBitmap(位图);
    尝试{
        附寄();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
}}


解决方案

我不完全理解你的要求,但会在 LocalBroadcastmanager 你需要什么?

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

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

这篇关于开始活动与非活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆