我有Android代码,可以创建图像并保存它们。对于大多数用户来说一切正常,但是对于少数人来说,他们无法查看图片,可能的原因是什么?
以下是我创建图片的代码段
public static String takeScreenshot(View view, boolean temp) {
String name;
if (temp) {
name = "last_shared";
} else {
Date now = new Date();
name = (String) android.text.format.DateFormat.format("yyyyMMdd_hhmmss", now);
}
// create directory if it does not exist
File folder = new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +
"/Quoted");
if (!folder.exists()) {
// NOTE: Should this be mkdirs()?
folder.mkdir();
}
try {
// image naming and path to include sd card appending name you choose for file
// String mPath = Environment.getExternalStorageDirectory().toString() + "/Quoted/quote_" + name + ".jpg";
String mPath = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.toString() + "/Quoted/quote_" + name + ".jpg";
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
return mPath;
//openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
return "";
}
public static void refreshGallery(String filename, Context context) {
MediaScannerConnection.scanFile(context,
new String[]{filename}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.d("ExternalStorage", "Scanned " + path + ":");
Log.d("ExternalStorage", "-> uri=" + uri);
}
}
);
}
问题出现在小米Redmi 3S Android 6.0和华硕ZenFone 2 Android 6.0等设备上。
我在其他6.0设备上测试了应用程序,但效果很好。
据我所知,原因可能是:
1. Environment.DIRECTORY_PICTURES
无法解决这些手机问题。那么我应该使用mkdirs()
吗?
2.无论我做什么,这是正确的方法吗?
如果有人遇到类似问题,请帮助我。我会喜欢任何暗示。感谢。