保存并显示在Android

时间:2016-09-19 16:09:31

标签: android android-camera

我正在尝试用相机拍照并在活动结果方法中我要保存它并启动一个新活动以显示我保存它的照片,但是imageView保持空白,我使用了代码from android developer 这是我的代码:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

private File createImageFile () throws IOException   {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        //Bundle extras = data.getExtras();
       // Bitmap imageBitmap =(Bitmap) extras.get("data");


        Intent formIntent= new Intent(MainActivity.this,FormActivity.class);
       // formIntent.putExtra("img",imageBitmap);
        formIntent.putExtra("path",mCurrentPhotoPath);
        startActivity(formIntent);
    }
}

显示照片的活动

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

    String path = getIntent().getStringExtra("path");
    mImageView = (ImageView) findViewById(R.id.imageView);

    File imgFile = new  File(path);
    Toast.makeText(this, path, Toast.LENGTH_LONG).show();

    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());


        mImageView.setImageBitmap(myBitmap);


    }
}

0 个答案:

没有答案
相关问题