我不斷收到錯誤。
每次我在模擬器中運行代碼時,它都會顯示該目錄已創建的'Toast',但是在該代碼行之後不久就會出現錯誤。在出現的錯誤是:無法在模擬器上打印屏幕截圖到sdcard(日食)
「/storage/sdcard/Pictures/screenshot.png:打開失敗:ENOENT(沒有這樣的文件或目錄)」
我有地方下面的相關代碼。
<manifest
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"
/>
</manifest>
public class myActivity {
private void openScreenPrint() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)){
View v1 = findViewById(R.id.myRelativeLayout).getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap myBM = Bitmap.createBitmap(v1.getDrawingCache());
saveBitmap(myBM);
v1.setDrawingCacheEnabled(false);
}
else{
Toast.makeText(this, "No Permission to Write", Toast.LENGTH_SHORT).show();
}
}
public void saveBitmap(Bitmap bitmap) {
FileOutputStream fos = null;
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File dir = new File(filePath);
if (!dir.exists()){
dir.mkdirs();
Toast.makeText(this, "created", Toast.LENGTH_LONG).show(); //This line shown every time
}
String fileName = "screenshot" + ".png";
File imagePath = new File(filePath, fileName);
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
//Log.e("Err", e.getMessage(), e);
} catch (IOException e) {
//Log.e("Err", e.getMessage(), e);
}
}
}
是的,我已經在您顯示的位置添加了100個MiB。它似乎沒有改變錯誤。 – user3527696
您的AVD正在運行哪個API? – scottt
目標:Android 4.4.2-API等級19 – user3527696