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

在尝试使用Java保存Bitmap到设备相册时遇到了NullPointerException。问题出现在`File.fixSlashes()`和`File()`构造方法中。代码尝试创建一个名为'Folder'的图片文件夹,并保存图片。如果文件夹不存在,它会尝试创建。然后,它将图片以JPEG格式压缩并写入文件。当保存失败时,会执行其他操作。最后,通过发送广播通知媒体扫描器来更新文件系统。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值