从Uri

时间:2018-05-08 17:52:53

标签: android android-intent imageview uri

private File createImageFile() {
        File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timeStamp = sdf.format(new Date());

        File imageFile = new File(picturesDirectory, "picture" + timeStamp +".jpg");

        SharedPreferences fileLocation = getSharedPreferences("filePath", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = fileLocation.edit();
        editor.putString("file", imageFile.toString());
        ImageView photoImg = (ImageView)findViewById(R.id.photoImg);
        photoImg.setImageURI(Uri.fromFile(imageFile));
        editor.commit();

        return imageFile;
    }

我正在尝试显示使用相机意图拍摄的图像,但是,在拍摄照片后,图像不会显示在我的ImageView中。图像肯定会保存,因为我可以在我的图库中查看它。

我已经浏览了stackoverflow并且没有任何答案似乎对我有用,我尝试过使用BitmapFactory但这也行不通。任何输入将不胜感激

2 个答案:

答案 0 :(得分:0)

使用可以帮助您的代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { 
        Bitmap photo = (Bitmap) data.getExtras().get("data");   
        imageView.setImageBitmap(photo); 
    }
} 

答案 1 :(得分:0)

private void takePhotoFromCamera() {

        str_SaveFolderName = Environment.getExternalStorageDirectory() + "/folder_name";
        str_randomnumber = String.valueOf(Calendar.getInstance().getTimeInMillis());
        wallpaperDirectory = new File(str_SaveFolderName);
        if (!wallpaperDirectory.exists())
            wallpaperDirectory.mkdirs();
        str_Camera_Photo_ImageName = str_randomnumber + ".jpg";
        str_Camera_Photo_ImagePath = str_SaveFolderName + "/" + str_randomnumber + ".jpg";

        System.err.println(" str_Camera_Photo_ImagePath  " + str_Camera_Photo_ImagePath);

        f = new File(str_Camera_Photo_ImagePath);
        startActivityForResult(new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE).
                putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)), Take_Photo);

    }

和onActivityResult() -

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == this.RESULT_CANCELED) {
            return;
        }

        if (requestCode == Take_Photo) {

            imageview.setImageBitmap(
                    decodeSampledBitmapFromResource(getResources(),
                            R.id.simpleImageView, 100, 100));
        }
}
相关问题