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搞定



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值