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

I got an error during crashes in the console and anrs. This error sometimes appears, and I can not find where the problem is.

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

Function code to save image:

  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; } 
+7
source share
2 answers

Try the following:

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

The fact is that getExternalStorageDirectory() returns a File . You need to get the absolute path to this file and merge it using "/Pictures/MyFolder" .

+3
source

with the same problem. I check my file name, then found zero, and quit

0
source

All Articles