我尝试保存位图,但第outStream = new FileOutputStream(file);
行始终抛出错误java.io.FileNotFoundException: /storage/emulated/0/inpaint/card.png: open failed: ENOENT (No such file or directory)
。
//Save bitmap
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
File f3=new File(Environment.getExternalStorageDirectory()+"/inpaint/");
if(!f3.exists())
f3.mkdirs();
OutputStream outStream = null;
File file = new File(Environment.getExternalStorageDirectory() + "/inpaint/"+"card"+".png");
try {
outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, outStream);
outStream.close();
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
我还添加了
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
到清单。
答案 0 :(得分:2)
如果您的设备包含棉花糖,请提供许可运行时间:
;with ServerPermsAndRoles as
(
select
spr.name as principal_name,
spr.type_desc as principal_type,
spm.permission_name collate SQL_Latin1_General_CP1_CI_AS as security_entity,
'permission' as security_type,
spm.state_desc
from sys.server_principals spr
inner join sys.server_permissions spm
on spr.principal_id = spm.grantee_principal_id
where spr.type in ('s', 'u')
union all
select
sp.name as principal_name,
sp.type_desc as principal_type,
spr.name as security_entity,
'role membership' as security_type,
null as state_desc
from sys.server_principals sp
inner join sys.server_role_members srm
on sp.principal_id = srm.member_principal_id
inner join sys.server_principals spr
on srm.role_principal_id = spr.principal_id
where sp.type in ('s', 'u')
)
select *
from ServerPermsAndRoles
order by principal_name
通过以下方法传递位图:
private static final int PERMISSION_REQUEST_CODE = 1;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED) {
Log.d("permission", "permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
}
答案 1 :(得分:1)
您可以应用以下代码 -
final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (!checkIfAlreadyhavePermission()) {
ActivityCompat.requestPermissions(YourActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
your_method();
}
} else {
your_method();
}
private void your_code(){
//Your entire code will go here
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
your_method();
} else {
Toast.makeText(getApplicationContext(), "Please provide access to external storage", Toast.LENGTH_LONG).show();
}
break;
}
}
}
答案 2 :(得分:1)
我认为你和我有同样的问题。在Android版本23或更高版本中,您需要向用户询问代码中的额外存储权限。详情请见以下链接。
http://www.androidhowtocode.com/2017/01/javaiofilenotfoundexception.html
答案 3 :(得分:1)
您可以使用此单行将图像保存在图片文件夹中,并将位图传递到以下行:
MediaStore.Images.Media.insertImage(getApplicationContext()。getContentResolver(),bitmap,“Detected Image”,“”);