1

我的应用程序有一个相机按钮。单击此按钮后,将启动本机相机应用程序,用户可以拍照。如果用户接受图片(点击复选标记),我想启动一个新活动,该活动将在新屏幕上显示该图像。我正在尝试像这样保存图像: File file = new File(this.getFilesDir(), filename); 然后获取文件路径并将路径作为字符串传递给我的新活动。在我的新活动中,我试图从先前的意图中检索文件路径,并使用它将图像加载到 ImageView 中。在我编写将图像加载到 ImageView 的位之前,我想通过在新屏幕上的文本区域中打印来检查文件路径字符串是否正在传递。不幸的是,它似乎没有正确通过。任何帮助将不胜感激。下面是我的代码片段。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_use_camera);

        // create Intent to take a picture and return control to the calling application
        Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        //start the image capture Intent
        startActivityForResult(cameraintent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    String filename = "myfile";

        //make sure the user clicked to accept the picture in the native camera app
        if (resultCode == RESULT_OK) {
           if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
            { 
                //save image internally 
                File file = new File(this.getFilesDir(), filename);
                //get the filepath
                String filepath = file.getAbsolutePath();
                //create the swatchintent
                Intent Swatchintent = new Intent(this, DisplaySwatchActivity.class);
                //pass filepath to the intent
                Swatchintent.putExtra(EXTRA_MESSAGE, filepath);

                //start the intent
                startActivity(Swatchintent);
            }
        }   
    }

然后,我的下一个活动:

public class DisplaySwatchActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_swatch);
        String filepath = this.getIntent().getStringExtra(UseCameraActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(20);
        textView.setText(filepath);

    }
4

0 回答 0