为什么MediaStore.Images.Media.insertImage总是将图像保存在"图片"



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

if(requestCode == SAVE_IMAGE) {
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + R.string.app_name + "/";
File dir = new File(path);
if(!dir.exists()){
dir.mkdirs();
}
OutputStream out;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File photoFile = new File(path, timeStamp + ".jpg");
photoFile.createNewFile();
out = new FileOutputStream(photoFile);
Bitmap full_image_result = full_image.copy(full_image.getConfig(), true);
selectedFilter.apply(full_image_result);
full_image_result.compress(Bitmap.CompressFormat.JPEG, 90, out);
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();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), R.string.noPermissions, Toast.LENGTH_LONG).show();
}
}

不知怎么的,路径是错误的,用Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictura/"替换了它,现在它可以工作了。我还用替换了insertImage调用

MediaScannerConnection.scanFile(this, new String[]{photoFile.getAbsolutePath()}, new String[]{"image/jpg"}, null);

,正如CommonsWare建议的那样。

相关内容

  • 没有找到相关文章

最新更新