在我的应用中,用户可以将屏幕截图和文件保存在外部存储中。但是当外部存储不可写时我该怎么办?将设备从棒棒糖更新为棉花糖时,我遇到了这个问题。当我试图保存屏幕截图时,我的应用程序无法识别SD卡。但它可以通过ES Filex Explorer访问,但只读!内部存储的一部分是否应该在每台设备上保留为“外部”存储?为什么这不起作用?
P.S。当外部存储可写时,代码运行良好!
这是我的代码:
Bitmap bitmap = getScreenshotFromRecyclerView(recyclerView);
File file = storeImage(bitmap, name);
if(file != null){
Toast.makeText(this, getString(R.string.screenshotSaved, name), Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(file.toURI().toString()), "image/*");
startActivity(intent);
}else{
Toast.makeText(this, getString(R.string.notAvailable), Toast.LENGTH_LONG).show();
}
private File storeImage(Bitmap image, String name) {
File pictureFile = getOutputMediaFile(name);
if (pictureFile == null) {
System.out.println("Error A");
}else{
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
System.out.println("Error B");
} catch (IOException e) {
System.out.println("Error C");
}
}
return pictureFile;
}
private File getOutputMediaFile(String name){
File mediaStorageDir = new File(new Helper(this).getPathToAppFolder());
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name#
File mediaFile;
if(!name.equals(getString(R.string.training))){
String timeStamp = new SimpleDateFormat("dd.MM.yyyy_HH:mm", Locale.getDefault()).format(new Date());
String mImageName= name + "_" + timeStamp + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
}else{
String mImageName= name + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
}
return mediaFile;
}