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

徘徊边缘 提交于 2019-12-05 23:50:12

问题


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

回答1:


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".



来源:https://stackoverflow.com/questions/31172711/android-java-io-file-fixslashesfile-java185

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!