我正在从URL下载图像并尝试通过通知按钮与Intent共享。
我在Manifest中设置了读写权限,并将其检查为Marshmallow设备。我接受存储权限但我无法共享图像,因为它不会保存文件。为什么呢?
我的代码:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE) {
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
String[] splitLink = link.getText().toString().split("/");
String imageName = splitLink[splitLink.length-1];
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageName);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
saveImage.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush(); // Optimizació: per borrar fluxe de bytes de sortida i obligar als que estan en memoria a escriures
fileOutputStream.close();
file.setReadable(true, false); // Per permetre acces de lectura a totes les aplicacions
if (!file.mkdirs()) Log.e("Crear carpeta 3", "No rutlla");
} catch (Exception e) {Log.e("Guardar imatge 3", "No rutlla"); e.printStackTrace();}
}
}
}
例外:
E/Guardar imatge 3: No rutlla
System.err:java.io.FileNotFoundException:/storage/emulated/0/Pictures/01.jpg: open failed: ENOENT (No such file or directory)