简体   繁体   English

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

[英]Why can't my Android app read from storage?

I am relatively new to Android, and I am trying to integrate a file explorer into my app. 我是Android的新手,我正在尝试将文件浏览器集成到我的应用程序中。 I am using this library, added via maven repository: https://android-arsenal.com/details/1/2690 我正在使用通过maven存储库添加的该库: 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! 我已经在清单文件中包含了READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限,但是由于某些原因,我的应用程序将整个外部存储读取为空目录,并且由于我的生命,我无法弄清楚原因!

When I use another app, like ES File Explorer for example, and navigate to the same directory , it is not even close to empty! 当我使用另一个应用程序(例如ES File Explorer)并导航到相同目录时 ,它甚至没有接近空! The directory is not empty, and other apps can read from it, but not mine, for some reason. 该目录不为空,由于某些原因,其他应用程序可以读取该目录,但不能读取我的目录。

activity_main.xml 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 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 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. 如果您以API 23为目标,则必须要求用户具有读/写权限才能访问外部存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么无法更新连接到我的Android应用程序的MySQL数据库? - Why can't a MySQL database, connected to my android app, be updated? 为什么我无法在Android应用程序中开始活动? - Why i can't start activity in my android app? 无法从文件读取我的课程(Android) - Can't read my class from file (Android) 如何使用android studio从应用程序中的内部或外部存储(SD卡)读取文件? - How can I read Files from Inter or External Storage (SD Card) in app using android studio? 从Android应用程序中的Google Cloud Storage中读取文件 - Read File from Google Cloud Storage within Android App 为什么这个Android应用程序无法将Firebase中的文本转换为字符串? - Why can't this Android app cast text from Firebase into strings? 当我在 onCreate 方法中编写必要的代码时,为什么我的 Android 应用程序没有要求用户允许存储权限? - Why isn't my Android app asking the user to allow storage permissions when I wrote the necessary code in the onCreate method? Android - 如何从外部存储读取文本文件 - Android - How can I read text files from external storage 即使有存储权限也无法从 SD 卡读取 - Can't read from sdcard even with storage permissions 为什么我不能写入 Android 外部存储? - Why can't I write to Android External Storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM