我想在我的sd卡上存储google地图截图co写了这段代码,但我不知道我是否误解了外部存储这个词,或者我做错了什么因为一切正常但截图保存在我的内部手机中记忆而不是SD卡
public void snapshot(View view) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if(mExternalStorageWriteable){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/DCIM/100MEDIA/");
dir.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
final File file = new File(dir, "MapScreen"+timeStamp+".png");
GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
bitmap = snapshot;
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
};
mMap.snapshot(callback);
}
}
我正在使用Htc One M8s
答案 0 :(得分:0)
外部存储是世界可读的任何存储,不一定是SD卡(或其他可移动设备)。内部存储是默认情况下对应用程序而言是私有的存储。
指定可移动外部存储要复杂得多,因为许多设备制造商使用不同的目录结构。尝试答案here。