我有一个Android应用程序将文件写入内部存储中的目录,但它只对某些API正确执行。它在使用Android 5.1(API 22)的手机上运行良好。在Nexus 5仿真器(API 21)上。但是,在API> = 23的大多数平板电脑中,它无法获取目录。它将文件写入单独的活动中,并将它们放在listView中以便在此活动中显示。每当此活动加载时,它都不会获取任何文件,因为目录路径'不存在。是的,我在清单中拥有所有读/写外部存储权限。如果有人真的需要看到它,我已经包含了活动的完整代码,但是这里重要的部分只是最后一个日志标记。
public class ListResults extends Activity {
int data_block = 100;
ArrayList<String> arraylist;
String path_string = Environment.getExternalStorageDirectory().getAbsolutePath()+"/TrafficCounter";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_results);
ListView listView1 = (ListView) findViewById(R.id.list);
File path = new File(path_string);
path.mkdirs();
String dir = "" + path;
Log.d("TAG","Path exists: " + path.exists());
Log.d("TAG",String.valueOf(path.mkdirs()));
Log.d("TAG",path.toString());
File f = new File(dir);
String[] fileList = f.list();
Log.d("TAG", Arrays.deepToString(fileList));
//////////////////////////////Rest of Activity (Not Relevant)///////////////////////////////////
arraylist= new ArrayList<String>();
if(fileList!=null){
for(int i=0;i<fileList.length;i++)
{
arraylist.add(fileList[i]);
}}
Collections.sort(arraylist);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arraylist);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String filename = ((TextView) view).getText().toString();
//Creating dialog for choosing what to do with files
final Dialog dialog = new Dialog(ListResults.this);
dialog.setContentView(R.layout.file_options_menu);
dialog.show();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
TextView nameDisplay = (TextView)dialog.findViewById(R.id.file_name);
nameDisplay.setText(filename);
//Defining all buttons within the dialog
Button viewButton = (Button)dialog.findViewById(R.id.view_button);
Button shareButton = (Button)dialog.findViewById(R.id.share_button);
Button deleteButton = (Button)dialog.findViewById(R.id.delete_button);
Button cancelButton = (Button)dialog.findViewById(R.id.cancel_button);
//View option
viewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File path = new File(path_string);
File file = new File(path + "/" + filename);
String[] loadText = Load(file);
String finalString = "";
for (int i = 0; i < loadText.length; i++) {
finalString += loadText[i] + System.getProperty("line.separator");
}
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("product", finalString);
i.putExtra("filename export", filename);
dialog.cancel();
startActivity(i);
finish();
}
});
//Upload option from within the dialog
shareButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
File path = new File(path_string);
File file = new File(path + "/" + filename);
Uri uri = Uri.fromFile(file);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent,
getResources().getText(R.string.chooser_text)));
dialog.cancel();
}
});
//Delete button function
deleteButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
File path = new File(path_string);
File file = new File(path,filename);
file.delete();
Toast.makeText(getApplicationContext(), "File Deleted", Toast.LENGTH_SHORT).show();
dialog.cancel();
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
//Cancel option from dialog
cancelButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
dialog.cancel();
}
});
}
});
}
public static String[] Load(File file)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
}
catch (FileNotFoundException e) {e.printStackTrace();}
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String test;
int anzahl=0;
try
{
while ((test=br.readLine()) != null)
{
anzahl++;
}
}
catch (IOException e) {e.printStackTrace();}
try
{
fis.getChannel().position(0);
}
catch (IOException e) {e.printStackTrace();}
String[] array = new String[anzahl];
String line;
int i = 0;
try
{
while((line=br.readLine())!=null)
{
array[i] = line;
i++;
}
}
catch (IOException e) {e.printStackTrace();}
return array;
}
public void onRestart(View view) {
Intent restart = new Intent(this, MainActivity.class);
startActivity(restart);
}
}
我包含了我为以下每个测试获得的日志标记。第一个是目录是否存在,第二行是mkdirs();使用了,第三个是实际地址,第四个是目录中的实际文件数组。 对于Nexus 5仿真器(API 21,有效):
D/TAG: Path exists: true
D/TAG: false
D/TAG: /storage/sdcard/TrafficCounter
D/TAG: [test.txt]
对于我的实际手机(API 22,有效):
D/TAG: Path exists: true
D/TAG: false
D/TAG: /storage/emulated/0/TrafficCounter
D/TAG: [Test.txt]
对于Nexus 7和Nexus S仿真器(API 23,不起作用):
D/TAG: Path exists: false
D/TAG: false
D/TAG: /storage/emulated/0/TrafficCounter
D/TAG: null
有谁知道如何正确获取更高API的目录?或者我完全错过了其他什么?