首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试写入Android sdcard时的java.io.FileNotFoundException (权限被拒绝)

尝试写入Android sdcard时的java.io.FileNotFoundException (权限被拒绝)
EN

Stack Overflow用户
提问于 2011-01-28 09:42:09
回答 6查看 65.3K关注 0票数 17

我正在尝试从照片库中选择一个图像文件并将其写入SD卡。下面是导致异常的代码。它似乎在尝试创建FileOutputStream时抛出此异常。我将以下行添加到嵌套在application元素中的清单文件中。我找不到解决这个问题的办法:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

代码语言:javascript
复制
public boolean saveSelectedImage( Uri selectedImage, int imageGroup,
        int imageNumber )
{
    boolean exception = false;
    InputStream input = null;
    OutputStream output = null;
    if( externalStorageIsWritable() )
    {
        try
        {
            ContentResolver content = ctx.getContentResolver();
            input = content.openInputStream( selectedImage );
            if(input != null) Log.v( CLASS_NAME, "Input Stream Opened successfully");
            File outFile = null;

            File root = Environment.getExternalStorageDirectory(  );
            if(root == null) Log.v(CLASS_NAME, "FAILED TO RETRIEVE DIRECTORY");
            else Log.v(CLASS_NAME, "ROOT DIRECTORY is:"+root.toString());

            output = new FileOutputStream( root+"/Image"+ imageGroup + "_" + imageNumber + ".png" );

            if(output != null) Log.e( CLASS_NAME, "Output Stream Opened successfully");
            //  output = new FileOutputStream
            // ("/sdcard/Image"+imageGroup+"_"+imageNumber+".png");

            byte[] buffer = new byte[1000];
            int bytesRead = 0;
            while ( ( bytesRead = input.read( buffer, 0, buffer.length ) ) >= 0 )
            {
                output.write( buffer, 0, buffer.length );
            }
        } catch ( Exception e )
        {

            Log.e( CLASS_NAME, "Exception occurred while moving image: ");
            e.printStackTrace();

            exception = true;
        } finally
        {
            // if(input != null)input.close();
            // if(output != null)output.close();
            // if (exception ) return false;
        }

        return true;
    } else
        return false;

}
EN

回答 6

Stack Overflow用户

发布于 2011-10-13 18:57:54

清单文件应该是这样的

代码语言:javascript
复制
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".WriteCBTextToFileActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

票数 24
EN

Stack Overflow用户

发布于 2013-03-06 03:03:43

我有这个问题,这里的答案没有解决它,因为我犯了一个相当愚蠢的错误。

确保你给你的Android模拟器一个SD卡…我已经把SD卡的大小留空了,所以它当然找不到任何东西。最重要的是,我在SD卡中指定了一个尚未创建的目录,所以一定要考虑到这种情况。

(您可以通过转到Android虚拟设备管理器,编辑您选择的模拟器,并输入SD卡大小的一些值来更改AVD的SD卡设置)

票数 9
EN

Stack Overflow用户

发布于 2012-09-21 16:22:52

如果您在清单中设置了正确的权限,请确保您的设备在连接到PC时未处于“磁盘驱动器”模式。将其更改为“仅收费”模式,它应该可以工作。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4823992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档