我正在使用this site找到的文件选择器。
当我浏览我放在设备上的文件(无论是模拟还是我的Nexus 5)时,它们都不会显示出来。当我浏览/sdcard/
时,它没有说明目录或文件。我知道我已经在那里推了几个.txt
和.xml
文件,默认情况下应该有多个目录。我几乎完全使用这些文件。
关于问题可能是什么想法?
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class FilePicker extends ListActivity {
public final static String EXTRA_FILE_PATH = "file_path";
public final static String EXTRA_SHOW_HIDDEN_FILES = "show_hidden_files";
public final static String EXTRA_ACCEPTED_FILE_EXTENSIONS = "accepted_file_extensions";
private final static String DEFAULT_INITIAL_DIRECTORY = "/";
protected File Directory;
protected ArrayList<File> Files;
protected FilePickerListAdapter Adapter;
protected boolean ShowHiddenFiles = false;
protected String[] acceptedFileExtensions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflator = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View emptyView = inflator.inflate(R.layout.empty_view, null);
((ViewGroup) getListView().getParent()).addView(emptyView);
getListView().setEmptyView(emptyView);
// Set initial directory
Directory = new File(DEFAULT_INITIAL_DIRECTORY);
// Initialize the ArrayList
Files = new ArrayList<File>();
// Set the ListAdapter
Adapter = new FilePickerListAdapter(this, Files);
setListAdapter(Adapter);
// Initialize the extensions array to allow any file extensions
acceptedFileExtensions = new String[] {};
// Get intent extras
if(getIntent().hasExtra(EXTRA_FILE_PATH))
Directory = new File(getIntent().getStringExtra(EXTRA_FILE_PATH));
if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES))
ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false);
if(getIntent().hasExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS)) {
ArrayList<String> collection =
getIntent().getStringArrayListExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS);
acceptedFileExtensions = (String[])
collection.toArray(new String[collection.size()]);
}
}
@Override
protected void onResume() {
refreshFilesList();
super.onResume();
}
protected void refreshFilesList() {
Files.clear();
ExtensionFilenameFilter filter =
new ExtensionFilenameFilter(acceptedFileExtensions);
File[] files = Directory.listFiles(filter);
if(files != null && files.length > 0) {
for(File f : files) {
if(f.isHidden() && !ShowHiddenFiles) {
continue;
}
Files.add(f);
}
Collections.sort(Files, new FileComparator());
}
Adapter.notifyDataSetChanged();
}
@Override
public void onBackPressed() {
if(Directory.getParentFile() != null) {
Directory = Directory.getParentFile();
refreshFilesList();
return;
}
super.onBackPressed();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File newFile = (File)l.getItemAtPosition(position);
if(newFile.isFile()) {
Intent extra = new Intent();
extra.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath());
setResult(RESULT_OK, extra);
finish();
}
else {
Directory = newFile;
refreshFilesList();
}
super.onListItemClick(l, v, position, id);
}
private class FilePickerListAdapter extends ArrayAdapter<File> {
private List<File> mObjects;
public FilePickerListAdapter(Context context, List<File> objects) {
super(context, R.layout.list_item, android.R.id.text1, objects);
mObjects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
}
else
row = convertView;
File object = mObjects.get(position);
ImageView imageView = (ImageView)row.findViewById(R.id.file_picker_image);
TextView textView = (TextView)row.findViewById(R.id.file_picker_text);
textView.setSingleLine(true);
textView.setText(object.getName());
/*if(object.isFile())
imageView.setImageResource(R.drawable.file);
else
imageView.setImageResource(R.drawable.folder);*/
return row;
}
}
private class FileComparator implements Comparator<File> {
public int compare(File f1, File f2) {
if(f1 == f2)
return 0;
if(f1.isDirectory() && f2.isFile())
// Show directories above files
return -1;
if(f1.isFile() && f2.isDirectory())
// Show files below directories
return 1;
// Sort the directories alphabetically
return f1.getName().compareToIgnoreCase(f2.getName());
}
}
private class ExtensionFilenameFilter implements FilenameFilter {
private String[] Extensions;
public ExtensionFilenameFilter(String[] extensions) {
super();
Extensions = extensions;
}
public boolean accept(File dir, String filename) {
if(new File(dir, filename).isDirectory()) {
// Accept all directory names
return true;
}
if(Extensions != null && Extensions.length > 0) {
for(int i = 0; i < Extensions.length; i++) {
if(filename.endsWith(Extensions[i])) {
// The filename ends with the extension
return true;
}
}
// The filename did not match any of the extensions
return false;
}
// No extensions has been set. Accept all file extensions.
return true;
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hks003.chordtransposer" >
<!-- <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/> -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<activity
android:name=".CurrentKey"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewKey"
android:label="@string/title_activity_new_key"
android:parentActivityName=".CurrentKey" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="hks003.chordtransposer.CurrentKey" />
</activity>
<activity android:name=".FilePicker">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</activity>
</application>
</manifest>
要清楚,当我打开/ sdcard /时,它说没有文件或目录。这里有很多目录,但都没有出现。
答案 0 :(得分:1)
如果您需要AndroidManifest.xml中的外部存储,则需要添加读取或写入的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
更多:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
修改强>
权限必须位于<manifest>
标记内,而不是<aplication>
或<activity>
标记:http://developer.android.com/guide/topics/manifest/manifest-intro.html,http://developer.android.com/guide/topics/manifest/uses-permission-element.html
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hks003.chordtransposer" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CurrentKey"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewKey"
android:label="@string/title_activity_new_key"
android:parentActivityName=".CurrentKey" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="hks003.chordtransposer.CurrentKey" />
</activity>
<activity android:name=".FilePicker">
</activity>
</application>
</manifest>