如何在其他类中使用一个类数据输出

时间:2014-10-05 16:32:42

标签: java android

我有两个类HomeActivityEditActivity,下面是我的HomeActivity类的代码,我在其中从图库中获取图像并想要解码它,解码后我想要在EditActivity的按钮功能中使用该图像作为我的输入。

HomeActivity

  public class HomeActivity extends Activity implements OnClickListener  {
    private Button btnEdit, btnGallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        btnEdit = (Button) findViewById(R.id.btn_edit);
        btnEdit.setOnClickListener(this);

        btnGallery = (Button) findViewById(R.id.btn_gallery);
        btnGallery.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
        case R.id.btn_edit:
            intent = new Intent();
            intent.setType("image/*");

            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(
                    Intent.createChooser(intent, "Select Photo"),
                    MyConstants.TAKE_PHOTO);

            break;

        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void onActivityResult(final int requestCode, int resultCode,
            final Intent data) {
        System.out.println("Data come from: " + data);
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MyConstants.TAKE_PHOTO) {
            if (resultCode == RESULT_OK) {
                Uri selectedImageUri = data.getData();
                String selectedImagePath = getPath(selectedImageUri);
                try
                {
                InputStream is = HomeActivity.this.getContentResolver().openInputStream(selectedImageUri);
                final Bitmap bmInImg = BitmapFactory.decodeStream(is);
                mPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
                nPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
                bmInImg.getPixels(mPhotoIntArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());
                mCannyOutArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
                final Bitmap bmOutImg = Bitmap.createBitmap(bmInImg.getWidth(), bmInImg.getHeight(), Config.ARGB_8888);  
                bmOutImg.setPixels(mCannyOutArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());

                }
                catch (IOException e) {
                    e.printStackTrace(); 
                }

                // System.out.println("Image Path : " + selectedImagePath);
                Intent intent = new Intent(HomeActivity.this,
                        PhotoEditorActivity.class);
                intent.setData(Uri.parse(selectedImagePath));
                startActivity(intent); 
            }
        }

        else if (requestCode == MyConstants.APP_EXIT) {
            if (resultCode == Activity.RESULT_OK) {
                finish();
                System.exit(0);
            }

        }  
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

}

现在我想在ClickActivity的HomeActivity bmInImg中使用bmOutImgtry/catch点击按钮

@Override
    public void onClick(View view) 
    {
       switch (view.getId()) {
        case R.id.btn_camera:
            negative(bmInImg.getHeight(),bmInImg.getWidth(), mPhotoIntArray, mCannyOutArray);
            bmOutImg.setPixels(mCannyOutArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());
            break;
    }
   }

我想将它与上面的逻辑一起使用,因为我在try/catch点击的EditActivity中使用HomeActivity类中的btn_camera的整个代码,但它没有显示输出。还有其他任何逻辑。< / p>

谢谢

1 个答案:

答案 0 :(得分:-1)

您可以在HomeActivity类中使用静态变量来存储数据,然后使用HomeActivity.variableName在EditActivity中访问它