gpt4 book ai didi

java - 将图像保存到外部存储(如果可用)

转载 作者:行者123 更新时间:2023-12-01 12:49:24 24 4
gpt4 key购买 nike

我想将壁纸保存到外部存储(如果可用),否则将其保存到设备内部存储。我的代码适用于安装了外部存储的设备,但当外部存储不可用时会失败。我的简短代码发布在下面

  public FileOutputStream getOutStream(String fileName) throws FileNotFoundException{
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String sdpath = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
+ "/";
mSavePath = sdpath + "DroidPack";
File file = new File(mSavePath);
if (!file.exists()) {
file.mkdir();
}
File saveFile = new File(mSavePath, fileName);

return new FileOutputStream(saveFile);


}else{
String sdpath = mContext.getFilesDir().getPath() + "/";
mSavePath = sdpath + "DroidPack";
File file = new File(mSavePath);
if (!file.exists()) {
file.mkdir();
}
return mContext.openFileOutput(fileName , Context.MODE_WORLD_READABLE);
}
}

}

最佳答案

看看Android Documentation

尽管如此,你应该尝试一下:

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}

您只是检查一个条件!

希望对你有帮助!

关于java - 将图像保存到外部存储(如果可用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com