我遵循了Saving an image from ImageView into internal storage的回答,但仍然无法保存任何内容。我的代码在这里:
// Icon names in the order of icons to be display.
List<Map<String,String>> iconNames = [
{'unselected': 'home', 'selected': 'home-selected'},
{'unselected': 'community', 'selected': 'community-selected'},
{'unselected': 'post', 'selected': 'post-selected'},
{'unselected': 'notification', 'selected': 'notification-selected'},
{'unselected': 'settings', 'selected': 'settings-selected'},
];
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: ClipRRect(
borderRadius: BorderRadius.vertical(top: Radius.circular(15)),
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed, // add this
currentIndex: _currentIndex,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
items: [
for(int i = 0; i < iconNames.length; ++i)
BottomNavigationBarItem(
icon: SvgPicture.asset(
// icon name based on selection
iconNames[i][i == _currentIndex ? 'selected' : 'unselected'],
),
title: Text(''),
)
],
),
),
);
}
使用这种方法,我得到带有messae的IOExeception: public void buttonPickImage(View view) {
FileOutputStream fos;
bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
Random rng = new Random();
int n = rng.nextInt(1000);
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/BAC");
bool = dir.mkdir();
File file = new File(dir, "BAC_"+n+".jpg");
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(),"Image sauvegardée"+bool,Toast.LENGTH_SHORT).show();
}catch (java.io.IOException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(),"IOException: " + e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
我也尝试过将其保存到内部存储中,但对我不起作用: https://www.tutorialspoint.com/how-to-write-an-image-file-in-internal-storage-in-android 使用这种方法,程序可以运行,但是布尔型mkdir给我错误。
感谢您的帮助
答案 0 :(得分:0)
最后使用 Media Store 代替getExternalStorageDirectory作为
此方法在API级别29中已弃用。 为了提高用户隐私,不建议直接访问共享/外部存储设备。当应用程序定位到Build.VERSION_CODES.Q时,此方法返回的路径不再可供应用程序直接访问。通过迁移到Context#getExternalFilesDir(String),MediaStore或Intent#ACTION_OPEN_DOCUMENT之类的替代方案,应用程序可以继续访问共享/外部存储中存储的内容。
MediaStore也很有用,因为它允许您在Android Gallery应用中获取图像。
所以我的解决方法是:
ImageView imageView = findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "any_picture_name");
values.put(MediaStore.Images.Media.BUCKET_ID, "test");
values.put(MediaStore.Images.Media.DESCRIPTION, "test Image taken");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
outstream.close();
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
仍然感谢@blackapps向我解释了有关IOexception,mkdir和Toast的一些基本知识。反正还是有用的。