java.io.FileNotFoundException(拒绝)当试图写入到Android SD卡io、java、FileNotFoundException、SD

2023-09-07 14:04:03 作者:今晚你是我的人

我想选择从图片库的图像文件,并写入到SD卡。下面是code,它导致异常。这似乎试图创建的FileOutputStream时抛出此异常。我有以下行添加到嵌套应用元素中的清单文件。我无法找到一个解决问题的办法:

<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>

 公共布尔saveSelectedImage(URI selectedImage,INT imageGroup,        INT图像NUMBER){    布尔例外= FALSE;    输入的InputStream = NULL;    OutputStream的输出= NULL;    如果(externalStorageIsWritable())    {        尝试        {            ContentResolver的内容= ctx.getContentResolver();            输入= content.openInputStream(selectedImage);            如果(输入!= NULL)Log.v(CLASS_NAME,输入流成功打开);            文件不过outFile = NULL;            文件根= Environment.getExternalStorageDirectory();            如果(根== NULL)Log.v(CLASS_NAME,无法检索目录);            其他Log.v(CLASS_NAME,根目录是:+ root.toString());            输出=新的FileOutputStream(根+/图片+ imageGroup +_+图像NUMBER +png格式);            如果(输出!= NULL)Log.e(CLASS_NAME,输出流成功打开);            //输出=新的FileOutputStream            //(/sdcard/Image\"+imageGroup+\"_\"+imageNumber+\".p​​ng);            字节[]缓冲区=新的字节[1000];            INT读取动作= 0;            而((读取动作= input.read(缓冲液,0,buffer.length))GT; = 0)            {                output.write(缓冲液,0,buffer.length);            }        }赶上(例外五)        {            Log.e(CLASS_NAME,而动态图像时出现异常:);            e.printStackTrace();            例外= TRUE;        }最后        {            //如果(!输入= NULL)input.close();            //如果(!=输出空)output.close();            //如果(例外)返回false;        }        返回true;    }其他        返回false;} 

解决方案

这是你的清单文件应该怎么看起来像

    

 <应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>    <活动机器人:WriteCBTextToFileActivityNAME =              机器人:标签=@字符串/ APP_NAME>        &所述;意图滤光器>            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>        &所述; /意图滤光器>    < /活性GT;< /用途><使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/> 
3Dmax安装完成后,激活时出现File not found,如何处理

I am trying to select an image file from the photo gallery and write to the sdcard. Below is the code that results in an exception. It appears to throw this exception when trying to create the FileOutputStream. I have the following line added to the manifest file nested inside the application element. I can't find a solution to the problem:

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

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;

}

解决方案

This is how your manifest file should look like

<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" />