gpt4 book ai didi

android - java.io.FileNotFoundException (Permission denied) 尝试写入 Android sdcard 时

转载 作者:IT老高 更新时间:2023-10-28 23:18:16 29 4
gpt4 key购买 nike

我正在尝试从照片库中选择一个图像文件并写入 SD 卡。下面是导致异常的代码。尝试创建 FileOutputStream 时似乎会引发此异常。我将以下行添加到嵌套在应用程序元素内的 list 文件中。我找不到问题的解决方案:

<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;

}

最佳答案

这就是 list 文件的样子

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

关于android - java.io.FileNotFoundException (Permission denied) 尝试写入 Android sdcard 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4823992/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com