为什么我的Android应用程序无法从存储中读取?

时间:2016-03-02 02:46:26

标签: java android xml android-manifest android-external-storage

我对Android比较陌生,我正在尝试将文件浏览器集成到我的应用中。我正在使用这个库,通过maven存储库添加:https://android-arsenal.com/details/1/2690

我在我的清单文件中包含READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限,但由于某种原因,我的应用程序将整个外部存储读取为一个空目录,并且对于我的生活我无法弄清楚为什么!

当我使用其他应用程序时,例如ES文件资源管理器,并导航到同一目录,它甚至不是空的!该目录不是空的,其他应用程序可以从中读取,但不是我的,出于某种原因。

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>

1 个答案:

答案 0 :(得分:1)

您是如何尝试阅读该文件的?在此示例代码中,您只是创建一个文件对象,但不执行任何操作。如果要从文件中读取,则需要输入流:

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();

如果你的目标是API 23,你也必须要求用户获得访问外部存储的读/写权限。