I am relatively new to Android, and I am trying to integrate a file explorer into my app. I am using this library, added via maven repository: https://android-arsenal.com/details/1/2690
I have included the READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
permissions in my manifest file, yet for some reason, my app reads the entirety of external storage as being an empty directory, and for the life of me I cannot figure out why!
When I use another app, like ES File Explorer for example, and navigate to the same directory , it is not even close to empty! The directory is not empty, and other apps can read from it, but not mine, for some reason.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="datashift.mat.datashiftnfc.MainActivity"
android:background="#ff0000">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/dsheader"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose File"
android:id="@+id/browseButton"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:onClick="openFilePicker"/>
</RelativeLayout>
MainActivity.java
package datashift.mat.datashiftnfc;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.nbsp.materialfilepicker.ui.FilePickerActivity;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openFilePicker(View v){
Intent intent = new Intent(this, FilePickerActivity.class);
intent.putExtra(FilePickerActivity.ARG_SHOW_HIDDEN, true);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
File file=new File(filePath);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="datashift.mat.datashiftnfc"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.NFC"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
How are you trying to read the file? In this sample code you are simply creating a file object, but doing nothing with it. If you want to read from a file, you need an input stream:
FileInputStream fin = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis), 64 * 1024);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
If you target API 23 you have to ask the user for read/write permission to access external storage as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.