【问题标题】:Set an ImageView from a Uri从 Uri 设置 ImageView
【发布时间】:2018-05-09 12:24:04
【问题描述】:
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 但这也不起作用。任何意见将不胜感激

【问题讨论】:

  • 这个imageFile 是否存在? new File() 不创建文件。
  • 如果URI 有效。关注This answer
  • 运行这段代码有没有抛出异常?如果是这样的话,了解我们正在处理的内容会有所帮助。发生这种情况时检查 logcat,看看是否能找到任何错误或警告,如果发现任何相关信息,请告诉我们。
  • @ADM 图像文件确实存在于路径中,因为它在我的画廊中可用,我也能够通过应用程序共享图像,只是似乎无法让它显示在图像视图中

标签: android android-intent imageview uri


【解决方案1】:

使用对您有帮助的代码

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); 
    }
} 

【讨论】:

    【解决方案2】:
    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));
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 2021-04-19
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      相关资源
      最近更新 更多