我正在尝试使用以下代码将图像保存在公共存储目录中。但是,当这是保存到存储/模拟/ 0而不是公共图片文件夹。我已经在另一个应用程序中使用了基本上这个确切的代码,它运行良好。有谁知道为什么.getExternalStoragePublic目录没有返回accessible / Pictures /而是返回storage / emulated / 0?
我的清单文件中有“uses-permission android:name =”android.permission.WRITE_EXTERNAL_STORAGE“
File backupFile;
File appFolder;
String path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/CustomFolder";
appFolder = new File(path);
if (!appFolder.exists())
appFolder.mkdir();
String fileName = "picture.jpg"
backupFile = new File(appFolder, fileName);
FileOutputStream output = null;
try {
output = new FileOutputStream(backupFile);
output.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
if (null != output) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
没关系,我明白了。事实证明只有"使用权限android:name =" android.permission.WRITE_EXTERNAL_STORAGE"在Manifest文件中是不够的,我必须在它之前放置以下代码才能使它工作:
int CAMERA_PERMISSION_REQUEST_CODE = 2;
int result = ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result != PackageManager.PERMISSION_GRANTED){
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(getActivity().getApplicationContext(), "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},CAMERA_PERMISSION_REQUEST_CODE);
}
}