Android 为什么MediaStore.Images.Media.insertImage总是将图像保存在图片文件夹中?

Android 为什么MediaStore.Images.Media.insertImage总是将图像保存在图片文件夹中?,android,mediastore,Android,Mediastore,我想将图像保存在与我的应用程序名称对应的文件夹中,而不是默认的“图片”文件夹。以下是我在onRequestPermissionsResult中的代码: if(requestCode==保存图像){ if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){ 试一试{ String path=Environment.getExternalStorageDirectory().getAbsolutePath(

我想将图像保存在与我的应用程序名称对应的文件夹中,而不是默认的“图片”文件夹。以下是我在onRequestPermissionsResult中的代码:

if(requestCode==保存图像){
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
试一试{
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+“/”+R.String.app_name+“/”;
文件目录=新文件(路径);
如果(!dir.exists()){
dir.mkdirs();
}
输出流输出;
字符串时间戳=新的SimpleDateFormat(“yyyyMMdd_HHmmss”)。格式(新日期();
File photoFile=新文件(路径,时间戳+“.jpg”);
创建新文件();
out=新文件输出流(照片文件);
位图full\u image\u result=full\u image.copy(full\u image.getConfig(),true);
选择过滤器。应用(完整图像结果);
完整图像结果压缩(位图.CompressFormat.JPEG,90,输出);
MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver()、photoFile.getAbsolutePath()、photoFile.getName()、photoFile.getName());
out.flush();
out.close();
Toast.makeText(getApplicationContext(),R.string.savedMessage,Toast.LENGTH_LONG).show();
}捕获(IOE异常){
e、 printStackTrace();
}
}否则{
Toast.makeText(getApplicationContext(),R.string.noPermissions,Toast.LENGTH\u LONG.show();
}
}

不知何故,路径错误,将其替换为
环境.getExternalStorageDirectory().getAbsolutePath()+“/Pictura/”
,现在它可以工作了。我还将insertImage调用替换为

MediaScannerConnection.scanFile(这个,新字符串[]{photoFile.getAbsolutePath()},新字符串[]{“image/jpg”},null);

,正如Commonware建议的那样。

“我想将图像保存在与我的应用程序名称对应的文件夹中,而不是默认的“图片”文件夹”——然后删除
insertImage()
调用。你的代码的其余部分已经满足你的要求。如果我这样做,图像不会显示在库中…而且,有时图像会由于某种原因被放在库中名为“2131558439”的文件夹中“如果我这样做,图像不会显示在库中”——我现在可以在库中看到它,但在名为“2131558439”的文件夹中,仍然不是我想要的结果