繁体   English   中英

Android:无法从Android设备上的内部存储访问图像文件

[英]Android: Cannot access an image file from the internal storage on Android device

我试图将相机拍摄的图像保存到设备的内部存储中,然后将其用作ImageView,但是我无法访问该文件。 这都是出于教育目的。 我有一个按钮可以将相机应用程序称为子活动。

public void addListener() {
    Button b = (Button)findViewById(R.id.snap);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File picturesDirectory;
            picturesDirectory = getFilesDir();
            imageFile = new File(picturesDirectory, "passspoints_image");

            Log.d(DEBUGTAG, imageFile.getAbsolutePath());

            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
            startActivityForResult(i, PHOTO_TAKEN);
        }
    });
}

然后,我尝试访问该文件。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case PHOTO_TAKEN:
            Log.d(DEBUGTAG, imageFile.getAbsolutePath());
            Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            try {
                photo = rotateImageIfRequired(photo, Uri.fromFile(imageFile));
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(photo != null) {
                ImageView imageView = (ImageView)findViewById(R.id.imageView);
                imageView.setImageBitmap(photo);
            } else {
                Toast.makeText(this, "Unable to read photo file", Toast.LENGTH_LONG).
                        show();
            }
            break;
    }
}

这是debug标记的路径:

/data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image

这是错误消息:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image: open failed: ENOENT (No such file or directory)

在此之前,我设法使用外部存储来执行此操作,但是如果外部存储不可用,我想使用内部存储。

谢谢。

passspoints_image是图片名称吗? 如何为其添加扩展名,例如passspoints_image.jpg

还是您在AndroidManifest.xml文件中具有读/写权限?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM