[英]I am getting a File not found exception and I don't know why the path is correct
我正在編寫一個應用程序,通過ShareActionProvider在工具欄上點擊共享選項,它將允許用戶通過電子郵件向其發送歌曲。 但我不斷收到FileNotFoundException的異常,這里是代碼,異常是
java.io.FileNotFoundException:song.mp3:打開失敗: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();
}
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[]
,然后在getView()
方法中僅顯示該File
的getName()
。
另外, adapter.isEmpty()
沒有必要檢查adapter.isEmpty()
並設置空適配器。 如果適配器為空,則不會顯示任何數據。 如果要顯示一個空視圖,則ListView類具有setEmptyView
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.