Android sdcard读写权限问题之中的一个
2017-07-16 14:37
591 查看
博主在刚刚在学习过程中发现了一个关于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搞定
配置了该配置的提示无读写权限。
在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搞定
相关文章推荐
- 关于android读写sdcard的权限问题
- Android sdcard读写权限问题之一
- 如何修改Android中模拟器的sdcard权限的问题
- android打开sdcard读写文件、访问网络权限
- android sdcard访问权限问题
- android中读写sdcard遇到Environment.MEDIA_SHARED问题
- android.uid.system下解决sdcard读写问题
- Android_SDCard 的读写权限设置
- android sdcard 写入数据权限的问题
- android.uid.system下解决sdcard读写问题
- android.uid.system下解决sdcard读写问题
- Android 外置SDCard读写权限总结
- DDMS中sdcard权限问题解决
- 关于android sqlite database存储到sdcard上的问题
- Android下文件操作模式(含SDCard的读写)
- Android 读写SDcard (转)
- Android中对sd卡的读写权限问题
- Android下文件操作模式(含SDCard的读写)
- Android SDCard操作(文件读写,容量计算)
- Android 读写SDcard