Android sdcard读写权限问题之中的一个

博主在刚刚在学习过程中发现了一个关于android往sdcard读写的问题,

配置了该配置的提示无读写权限。

在AndroidManifest.xml文件里配置清单例如以下


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.custom"
    android:versionCode="1"
    android:versionName="1.0" >
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <provider
            android:name="custom_content_provider.RegionContentProvider"
            android:authorities="mobile.android.wang.hao.regioncontent" />
        <activity android:name="custom_content_provider.Main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


往sdcard写文件的代码例如以下

//打开数据库
private SQLiteDatabase openDatabase(){
Log.d("error", "openDatabase");
InputStream is = null;
FileOutputStream fos = null;
try{

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

//获取文件文件夹
String dbFileName = Environment.getExternalStorageDirectory()+"/region.db";
Log.d("error", dbFileName);
if(!(new File(dbFileName).exists())){ //文件不存在copy
is = getContext().getResources().getAssets().open("region.db");
fos = new FileOutputStream(dbFileName);
byte[] buffer = new byte[8192];
int count = 0;
while((count=is.read(buffer))>0){
fos.write(buffer,0,count);
}
}
}else{
Log.d("error", "无读写权限"+Environment.getExternalStorageDirectory()+"/region.db");
}
}catch(Exception ex){
Log.d("error", ex.getMessage());
}finally{
// 关闭流  略...
}
return null;


然后执行的时候提示无权限訪问该sdcard路径,可是我们配置的也配置了。网上有说是sdk版本号的问题,

说2.2以后的版本号不能用FileOutputStream 创建文件,搞了半天。还是一样,最后我用手机測试了一下,

发现文件创建成功,突然。我想了一下,是否有sdcard呢?



d---------,问题竟然出在这里,难道是虚拟机没有装载sdcard。紧接着,我重新启动了一把。OK搞定



原文地址:https://www.cnblogs.com/clnchanpin/p/7190490.html