我正在尝试从照片库中选择一个图像文件并将其写入SD卡。下面是导致异常的代码。它似乎在尝试创建FileOutputStream时抛出此异常。我将以下行添加到嵌套在application元素中的清单文件中。我找不到解决这个问题的办法:
<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;
}
发布于 2011-10-13 18:57:54
清单文件应该是这样的
<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" />
发布于 2013-03-06 03:03:43
我有这个问题,这里的答案没有解决它,因为我犯了一个相当愚蠢的错误。
确保你给你的Android模拟器一个SD卡…我已经把SD卡的大小留空了,所以它当然找不到任何东西。最重要的是,我在SD卡中指定了一个尚未创建的目录,所以一定要考虑到这种情况。
(您可以通过转到Android虚拟设备管理器,编辑您选择的模拟器,并输入SD卡大小的一些值来更改AVD的SD卡设置)
发布于 2012-09-21 16:22:52
如果您在清单中设置了正确的权限,请确保您的设备在连接到PC时未处于“磁盘驱动器”模式。将其更改为“仅收费”模式,它应该可以工作。
https://stackoverflow.com/questions/4823992
复制相似问题