我想在Android系统中使用文件浏览器来显示我之前保存的文件。
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
所以,我认为将数据保存到SD卡是实现这一目标的唯一方法。我发现了很多材料,但我总是得到错误,我没有权限将数据保存到SD卡中。错误消息如下。
01-17 23:35:44.184 6337-6337/? W/System.err: java.io.FileNotFoundException: /storage/emulated/0/rtd: open failed: EACCES (Permission denied)
01-17 23:35:44.184 6337-6337/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
01-17 23:35:44.185 6337-6337/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-17 23:35:44.185 6337-6337/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
事实上,我确实在AndroidManifest中添加了权限行
</activity>
</application>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
那么,任何人都可以帮我弄清楚我应该怎么做才能解决这个错误吗? 这是我的保存按钮的代码
class ButtonClickListener2 implements View.OnClickListener{
@Override
public void onClick(View v){
// Intent intent = getIntent();
Name = getIntent().getStringExtra("Name");
Age = getIntent().getStringExtra("Age");
Movie = getIntent().getStringExtra("Movie");
File2 = getIntent().getStringExtra("File");
TextView txt = (TextView)findViewById(R.id.textView3);
txt.setText(Name+" "+ Age+ " "+Movie+" "+ File2);
String filename = File2.toString();
String Age_Content = Age.toString();
String Movie_Content = Movie.toString();
String Name_Content = Name.toString();
FileService service = new FileService(getApplicationContext());
try {
if(Environment.getExternalStorageState().equals(Environment.getExternalStorageState())){
service.save(filename, Age_Content, Movie_Content,Name_Content);
Toast.makeText(getApplicationContext(),R.string.SDCard_available,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),R.string.SDCard_protected,Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(),R.string.success,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),R.string.fail,Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
class FileService {
private Context context;
public FileService (Context context) {
this.context = context;
}
public void save(String filename, String Age_content, String Movie_content, String Name_content) throws Exception{
//save the data into SD card
/* FileOutputStream outputStream = context.openFileOutput(filename,Context.MODE_APPEND);
outputStream.write(Age_content.getBytes());
outputStream.write(Movie_content.getBytes());
outputStream.write(Name_content.getBytes());
outputStream.close();*/
File file = new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(Age_content.getBytes());
outputStream.write(Movie_content.getBytes());
outputStream.write(Name_content.getBytes());
outputStream.close();
}
}
答案 0 :(得分:1)
如果您使用的是Android M,请尝试以下操作:
private final String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestPermissions(permissions, 2909);
另外,覆盖onRequestPermissionsResult
方法,如下所示:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
您的活动必须扩展AppCompatActivity才能实现此功能。 Android已经引入了6.0版的运行时权限。你可能想看看它。
答案 1 :(得分:0)
In your manifests file put the below code
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:0)
检查您是否在AndroidManifest.xml中使用了PERMISSIONS
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />