我正在编写一个应用程序,通过ShareActionProvider点击工具栏上的共享选项,它将允许用户通过电子邮件发送歌曲。但我一直得到FileNotFoundException的异常,这里是代码,例外是这个
java.io.FileNotFoundException:song.mp3:open failed:ENOENT(没有这样的文件或目录)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
menu.clear();
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.sidebar_menu, menu);
SubMenu subMenu = menu.addSubMenu("Themes");
subMenu.add(0 , blue , 0 , "Blue");
subMenu.add(0, pink , 1, "Pink");
items = subMenu.getItem().getItemId();
// tool bar menu
ArrayList<Uri> al = new ArrayList<Uri>();
ArrayList<Uri> emailAl = new ArrayList<Uri>();
ListView lv = (ListView)findViewById(R.id.downloads);
MenuItem mi = menu.findItem(R.id.searchable);
MenuItem share = menu.findItem(R.id.share);
mi.setIcon(android.R.drawable.ic_search_category_default);
SearchView searchView = (SearchView) menu.findItem(R.id.searchable).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
ShareActionProvider sap =(ShareActionProvider) MenuItemCompat.getActionProvider(share);
Intent intentShare = new Intent(Intent.ACTION_SEND_MULTIPLE);
Intent intentEmail = new Intent(Intent.ACTION_SEND_MULTIPLE);
intentShare.setType("audio/mp3");
intentEmail.setType("audio/mp3");
Uri uri = null;
Uri uriEmail = null;
try{
for(String file : mp3Files ){
File filein = new File(file);
uri = Uri.fromFile(filein);
FileInputStream in = new FileInputStream(filein.getName());
File outFile = new File(filein.getPath(), filein.getName()); // IMPORTANT! You need to create your file object separately, so you can then pass it to intent as well..
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len; while ( (len = in.read(buf, 0, buf.length)) != -1){
out.write(buf, 0, len);
}
in.close();
out.close();
uriEmail = Uri.fromFile(outFile); // Here you passed the parent directory file.. Pass newly created file object ..
al.add(uri);
emailAl.add(uriEmail);
}
} catch(IOException e){
e.printStackTrace();
}
String text = "";
intentShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM,al );
intentEmail.putParcelableArrayListExtra(Intent.EXTRA_STREAM,emailAl);
intentEmail.putExtra(Intent.EXTRA_SUBJECT , "Subject");
intentEmail.putExtra(Intent.EXTRA_TEXT , text);
sap.setShareIntent(intentShare);
sap.setShareIntent(intentEmail);
MenuItemCompat.OnActionExpandListener el = new MenuItemCompat.OnActionExpandListener(){
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when action item collapses
return true; // Return true to collapse action view
}
这是mp3files部分,其中mp3files被全局声明
private void ShowLists(){
files = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS).toString());
downloads = files.listFiles(new FileFilter(){
@Override
public boolean accept(File file){
String ext = file.getName();
if(ext.endsWith(".mp3")){
return true;
}
return false;
}
});
mp3Files = new String[downloads.length];
for(int i = 0; i < mp3Files.length; i++){
mp3Files[i] = downloads[i].getName();
}
adapter = new ArrayAdapter<String>(this,R.layout.downloads, R.id.textviewone , mp3Files);
ListView downloadsList = (ListView) findViewById(R.id.downloads);
downloadsList.setAdapter(adapter);
if(adapter.isEmpty()){
downloadsList.setAdapter(null);
} else {
downloadsList.setAdapter(adapter);
}
}
我也试过使用几种不同的方式获取下载目录,包括环境方式,但它仍然无效请帮助
已更新
所以我将代码更改为此代码仍然无效
try{
for(File file : mp3Files){
File filein = new File(file.toString());
uri = Uri.fromFile(filein);
FileInputStream in = new FileInputStream(filein);
File outFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filein.getName()); // IMPORTANT! You need to create your file object separately, so you can then pass it to intent as well..
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len; while ( (len = in.read(buf, 0, buf.length)) != -1){
out.write(buf, 0, len);
}
in.close();
out.close();
uriEmail = Uri.fromFile(outFile); // Here you passed the parent directory file.. Pass newly created file object ..
al.add(uri);
emailAl.add(uriEmail);
}
} catch(IOException e){
e.printStackTrace();
}
答案 0 :(得分:1)
mp3Files
只是文件名,而不是磁盘上文件的完整路径。
mp3Files[i] = downloads[i].getName();
您正在获取FileNotFound,因为您只能使用名称new File()
并期望它知道在哪里查找该文件。
需要这样,但正如您所见,mp3Files
无需使用。
mp3Files[i] = downloads[i];
换句话说,mp3Files = files.listFiles(...)
然后,这个循环可能只是for (File file : mp3Files)
for(String file : mp3Files ){
File filein = new File(file);
uri = Uri.fromFile(filein);
如果您真的希望适配器只显示文件的名称,那么您可以实现自己的ArrayAdapter类来接受File[]
,然后只显示getName()
File
在getView()
方法中。
此外,检查adapter.isEmpty()
并设置空适配器并不是必需的。如果适配器为空,则不显示任何数据。如果要显示空视图,则ListView类具有setEmptyView
方法。