按意图和保存的温度拍摄的照片,如何在imageview中显示?

时间:2018-08-28 09:17:54

标签: android

试图弄清楚如何临时保存图像。我找到了这段代码,它对我有用,但是在我拍照并按OK后,imageview中没有任何显示。我需要从相机拍摄并显示这些图片,直到将其上传

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 = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {

        }
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将图像存储在临时文件中后,您将拥有该文件的uri / path。

然后从该文件路径创建位图图像,如下所示:

   File sd = Environment.getExternalStorageDirectory();
   File image = new File(sd+filePath, imageName);
   BitmapFactory.Options bmOptions = new BitmapFactory.Options();
   Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
   bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);

imageView.setImageBitmap(bitmap);`

获得位图后,将其设置为图像视图。

imageView.setBitmapImage(bitmap);

您也可以将图像相关的库用于相同的目的。我会推荐PicassoGlide