【问题标题】:Android emulator open failed: ENOENT (No such file or directory)Android模拟器打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2016-10-02 16:29:54
【问题描述】:

我需要有关在 Android 模拟器中保存文件的权限的帮助...我已经添加了

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="true" />

在 AndroidManifest.xml 中

我的存档代码:

   if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageView.setImageBitmap(imageBitmap);
   }

        String root = Environment.getExternalStorageDirectory().toString();


        File myDir = new File(root + "/Img");
        myDir.mkdirs();
        long n = System.currentTimeMillis();
        String fname = "IMG_" + n + ".jpeg";
        file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        try {
            FileOutputStream out2 = new FileOutputStream(file);// here fire exception
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out2); 
            out2.flush();
            out2.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.denis.calculator">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".InfoActivity"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />


</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="true" />

有什么想法吗?

【问题讨论】:

  • 你到底有什么异常?
  • 打开失败:ENOENT(没有这样的文件或目录)
  • 删除这行if (file.exists()) file.delete();
  • 还是不行:/
  • 你在真机上试过了吗?这个问题只发生在模拟器上吗?

标签: android file android-emulator storage android-permissions


【解决方案1】:

解决方案是从 Nougat 降级为 KitKat!谢谢建议

【讨论】:

  • 是的!此解决方案无法进一步发挥作用,并且您的应用在新操作系统上的行为可能会模棱两可。
【解决方案2】:

替换下面的行

String root = Environment.getExternalStorageDirectory().toString();

File myDir = new File(root + "/Img");

File myDir = new File(Environment.getExternalStorageDirectory(),"Img");

同时从您的代码中删除以下行

if (file.exists())
    file.delete();

因为你定义文件名的方式永远不会有相同的文件名。所以在这里验证文件是否存在是无关紧要的。

如果您的应用将用于 Android 6.0 或更高版本,您还必须向用户征得写入权限。详情请参考以下链接:

https://developer.android.com/training/permissions/requesting.html

您可以使用库中定义的解决方案,我最近创建了这个存储库,包括权限演示。

https://github.com/eeshan-jamal/DroidLibX

稍后我将通过 Maven 使其可用,但现在您必须在项目中导入 droidlibx 库模块。

【讨论】:

  • 还是不行:/在这一行跳转到catch FileOutputStream out2 = new FileOutputStream(file);
  • 是的,我有 .. 在 nexus 上:/
  • 能否请您使用手机上的文件浏览器应用程序检查此 Img 目录是否在根目录中创建?
  • 好像没有
  • 您必须通过引用我的答案中的链接在您的应用中添加权限请求者模型。仅在 Manifest 上定义它不适用于 Android OS 版本的 Marshmallow、Nougat 和未来的版本。当前的解决方案仅在棒棒糖操作系统之前有效。您可以通过创建针对 Lollipop OS 的模拟器在模拟器上进行测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多