【问题标题】:Android: Cannot access an image file from the internal storage on Android deviceAndroid:无法从 Android 设备的内部存储访问图像文件
【发布时间】:2017-01-04 00:30:58
【问题描述】:

我正在尝试将相机拍摄的图像保存到设备的内部存储中,然后将其用作 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;
    }
}

这是调试标签的路径:

/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)

在此之前我已经设法使用外部存储来做到这一点,但我想使用内部存储以防外部存储不可用。

谢谢。

【问题讨论】:

    标签: android storage internal-storage image-file


    【解决方案1】:

    passspoints_image是图片名称吗?要不给它加个扩展,比如passspoints_image.jpg

    或者您的 AndroidManifest.xml 文件是否具有读/写权限?

    【讨论】:

    • 是的,passpoins_image 是文件名。正如我所提到的,当我使用外部存储时它工作正常,当picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 我在 android 清单中有权限时,但我认为它们仅用于读取/写入外部存储,而不是内部存储。无论如何,我有这个:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    • 不幸的是 .jpg 和 .bmp 仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2021-02-04
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多