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

前端之家收集整理的这篇文章主要介绍了android java.io.File.fixSlashes(File.java:185)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在控制台崩溃和错误中出错了ANR次数.有时会出现此错误,我无法找到问题所在.
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”连接.

猜你在找的Android相关文章