【问题标题】:Cannot print screenshot to sdcard on emulator (eclipse)无法在模拟器(eclipse)上将屏幕截图打印到 sdcard
【发布时间】:2014-04-12 21:15:30
【问题描述】:

我不断收到错误。
每次我在模拟器中运行代码时,它都会显示创建目录的“Toast”,但在该代码行之后不久肯定会出现错误。出现的错误是:

“/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);
    }
}
}

【问题讨论】:

    标签: android eclipse storage emulation screenshot


    【解决方案1】:

    您是否在 AVD 中创建了 SD 卡?如果没有,这是您在创建/编辑模拟设备时要使用的条目:

    【讨论】:

    • 是的,我在您显示的这个位置添加了 100 MiB。它似乎没有改变错误。
    • 您的 AVD 正在运行哪个 API?
    • 目标:Android 4.4.2-API 级别 19
    • 谷歌改变了外部存储的工作方式;他们严重限制了可以使用的位置。您可以查看 CommonsWare (commonsware.com/blog/2014/04/07/…) 的这篇文章了解一些背景知识。顺便说一句,您的代码在真实和虚拟 API 16 设备上运行良好,但在 API 19 AVD 上失败。
    • 该系列的第二篇文章可能更贴切:commonsware.com/blog/2014/04/08/…
    【解决方案2】:

    您实际上并不知道该目录实际上正在被创建。无论目录创建成功与否,您都在制作 Toast。您应该更改此部分:

    if (!dir.exists()){
        dir.mkdirs();
        Toast.makeText(this, "created", Toast.LENGTH_LONG).show();
    }
    

    测试目录是否实际创建成功。 File.mkdirs() 返回一个布尔值。

    if (!dir.exists()) {
        Toast.makeText(this, "dir not exists. attempting to create...", Toast.LENGTH_SHORT).show();
        if (dir.mkdirs()) {
            Toast.makeText(this, "dir created", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "dir creation failed", Toast.LENGTH_SHORT).show();
        }
    }
    

    【讨论】:

    • 外部检查是针对dir.exists() 的,因此在此处进行了介绍。但要为别人指出一件好事。
    猜你喜欢
    • 1970-01-01
    • 2017-03-24
    • 2022-01-22
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    相关资源
    最近更新 更多