gpt4 book ai didi

Android 文件选择器不显示文件

转载 作者:行者123 更新时间:2023-11-29 01:30:42 33 4
gpt4 key购买 nike

我正在使用在 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;
}
}
}

list :

<?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/时,它显示没有文件或目录。这里有很多目录,但没有一个显示出来。

最佳答案

需要在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>

关于Android 文件选择器不显示文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31329397/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com