gpt4 book ai didi

java - android java.io.File.fixSlashes(File.java :185)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:09 26 4
gpt4 key购买 nike

我在控制台崩溃和错误中遇到错误。有时会出现此错误,我找不到问题所在。

java.lang.NullPointerException
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)

保存图片的函数代码为:

    public static String sharePhoto(Context context, Bitmap bmp) {
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Folder");
boolean success = true;
String file_path = null;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
file_path = folder + "/Img_" + System.currentTimeMillis() / 1000 + ".jpg";
}
OutputStream os = null;
try {
os = new FileOutputStream(file_path);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, os);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Do something else on failure
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(file_path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);

return file_path;
}

最佳答案

试试这个:

File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/MyFolder");

事情是 getExternalStorageDirectory() 返回 File。您需要获取该文件的绝对路径并与 "/Pictures/MyFolder" 连接。

关于java - android java.io.File.fixSlashes(File.java :185),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com