android java.io.File.fixSlashes(File.java:185) [英] android java.io.File.fixSlashes(File.java:185)

查看:505
本文介绍了android java.io.File.fixSlashes(File.java:185)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台崩溃时遇到错误ANR次数。这个错误有时会显示,我不能找到问题的地方。

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


$ b

保存图片的函数代码是:

$ p $ public static String这个文件夹可以是一个文件夹,一个文件夹,一个文件夹,一个文件夹,一个文件夹,一个文件夹,一个文件夹,一个文件夹。
布尔成功=真;
String file_path = null;
if(!folder.exists()){
success = folder.mkdir();

if(success){
file_path = folder +/ Img_+ System.currentTimeMillis()/ 1000 +.jpg;
}
OutputStream os = null;
尝试{
os = new FileOutputStream(file_path);
bmp.compress(Bitmap.CompressFormat.JPEG,100,os);
} catch(IOException e){
e.printStackTrace();
}
} else {
//在失败时做其他事情
}
意图mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
文件f =新文件(file_path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);

返回file_path;


解决方案

试试这个:

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

这就是 getExternalStorageDirectory() code>文件。你需要获得该文件的绝对路径,并连接到/ Pictures / MyFolder


I got an error in console crashes & anrs. This error is showing sometimes and I couldn't find where the problem is.

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

The function code to save picture is:

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

解决方案

Try this:

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

Thing is that getExternalStorageDirectory() returns File. You need to get absolute path of that file and concatenate with "/Pictures/MyFolder".

这篇关于android java.io.File.fixSlashes(File.java:185)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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