Android getAbsolutePath() 不返回完整路径 [英] Android getAbsolutePath() not returning full path

查看:75
本文介绍了Android getAbsolutePath() 不返回完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建一个文件并将图像保存到其中:

I create a file and save an image to it using the following code:

private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getActivity().getApplicationContext().getFilesDir();
        File image = File.createTempFile(imageFileName, ".jpg", storageDir);
        return image;

    }

当我使用 image.getAbsolutePath(); 时,我得到了这样的东西:

When I use image.getAbsolutePath();, I get somwthing like this:

/data/data/co.za.package.app/files/filename.jpg

图片的实际路径为:

/storage/sdcard0/Android/data/co.za.package.app/files/filename.jpg

为什么 getAbsolutePath() 返回错误的路径?我对上面的 String 进行了硬编码,我的图像显示正常.你们中有人知道我做错了什么吗?提前谢谢您

Why is getAbsolutePath() returning the wrong path? I hardcoded the above String and my image displayed fine. Do any of you have any idea what I'm doing wrong? Thank you in advance

推荐答案

图片的实际路径为

The actual path of the image is

您可能在那里有一个文件,但这不是您在代码中设置的 File.尝试 getExternalFilesDir(null) 而不是 getFilesDir().

You may have a file there, but that is not the File that you are setting up in the code. Try getExternalFilesDir(null) instead of getFilesDir().

这篇关于Android getAbsolutePath() 不返回完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆