Android 如何在新布局中打印图像

Android 如何在新布局中打印图像,android,image,image-processing,Android,Image,Image Processing,我试图以新的布局打印图像,但不起作用 有人能帮我吗 代码如下: 在主要活动中: case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture startActivity(new Intent(MainActivity.this, Main2Activity.class));

我试图以新的布局打印图像,但不起作用 有人能帮我吗

代码如下:

在主要活动中:

case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture
                startActivity(new Intent(MainActivity.this, Main2Activity.class));

                Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
                startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
                break;
case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture

    Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
    startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
    break;

...

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
        case REQUEST_PICK_PIC:
            if (resultCode == RESULT_OK) {
                Uri imageUri = intent.getData();
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                intent.putExtra("IMAGE_URI", uri.toString());
                startActivity(intent);
                }
            break;

    }
}
在Main2活动中:

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
        case REQUEST_PICK_PIC:
            if (resultCode == RESULT_OK) {
                Uri imageUri = intent.getData();
                Bitmap bitmap;
                try {

                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                    iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;

    }
}
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
            Uri uri = Uri.parse(getIntent().getStringExtra("IMAGE_URI"));
            Bitmap bitmap;
                try {

                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    }
那么我的问题是什么


感谢您的帮助

在处理Main活动的OnActivity结果后,您必须启动Main2活动,如:

主要活动:

case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture
                startActivity(new Intent(MainActivity.this, Main2Activity.class));

                Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
                startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
                break;
case R.id.gallery: //in the moment that the user pressed the button pickAPic, the app will give him to choose a picture

    Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //the app ask access for the gallery
    startActivityForResult(pickImageIntent, REQUEST_PICK_PIC); // when he press on a pic
    break;

...

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
        case REQUEST_PICK_PIC:
            if (resultCode == RESULT_OK) {
                Uri imageUri = intent.getData();
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                intent.putExtra("IMAGE_URI", uri.toString());
                startActivity(intent);
                }
            break;

    }
}
在Main2活动中:

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
        case REQUEST_PICK_PIC:
            if (resultCode == RESULT_OK) {
                Uri imageUri = intent.getData();
                Bitmap bitmap;
                try {

                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                    iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;

    }
}
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
            Uri uri = Uri.parse(getIntent().getStringExtra("IMAGE_URI"));
            Bitmap bitmap;
                try {

                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    iv.setImageBitmap(bitmap); // the app "prints" the image that the user choose
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    }

因为在主活动中,我必须“从图库中选择”,我希望Main2活动将打印图像OK,因此您可以处理“onActivityResult”以在主活动中获取所选图像,然后启动“Main2活动”(所选图像与您传递给startActivity方法的意图一致)。这更好,因为当用户不选择图像时(通过点击后退按钮),您可以处理这种情况,并且我认为您不想在这种情况下启动Main2活动?您需要代码示例吗?