gpt4 book ai didi

java - 我收到文件未找到异常,我不知道为什么路径是正确的

转载 作者:行者123 更新时间:2023-12-01 09:23:23 25 4
gpt4 key购买 nike

我正在编写一个应用程序,当使用 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[],然后仅显示 getName() getView() 方法中该 File

此外,检查 adapter.isEmpty() 并设置空适配器并不是真正必要的。如果适配器为空,则不显示任何数据。如果你想显示一个空 View ,那么ListView类有一个setEmptyView方法。

关于java - 我收到文件未找到异常,我不知道为什么路径是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40010858/

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