2016-03-02 71 views
0

我对Android比较陌生,我正在尝试将文件资源管理器集成到我的应用程序中。我使用这个库,通过Maven仓库补充说:https://android-arsenal.com/details/1/2690为什么我的Android应用程序无法从存储中读取?

我已经包含在我的清单文件中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

你是如何试图读取该文件?在这个示例代码中,您只是简单地创建了一个文件对象,但却无所作为。如果你想从文件中读取,你需要一个输入流:

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,你要问的读/写权限的用户访问外部存储。

+0

该库打开文件资源管理器,该文件资源管理器应显示目录中的所有文件,文件对象是用户从该资源管理器中选择的文件,但资源管理器将外部存储显示为空。 –

+0

我并不熟悉该库,但可以尝试使用标准Android意图显示文件选取器: 意图intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(“file/*”); startActivityForResult(intent,PICKFILE_RESULT_CODE); – Francesc

+0

您有关于该主题的任何资源吗?这就是我想要做的,但我不知道如何 –

相关问题