您好我在写一个文件时遇到了一些麻烦,我在这里看了这个写 https://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage
但是,每次打开我发给自己的电子邮件时,我似乎都不明白我做错了什么,它说文件旁边有0.0b,并且不让我听到它或者预览它。任何人都可以帮助我理解为什么这个代码是这样的,而不是工作????
@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 dest = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
File filein = new File(file);
uri = Uri.fromFile(filein);
InputStream in = new FileInputStream(filein.getName());
FileOutputStream out = new FileOutputStream(new File(dest, filein.getName()));
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(dest);
al.add(uri);
emailAl.add(uriEmail);
intentShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM,al );
intentEmail.putParcelableArrayListExtra(Intent.EXTRA_STREAM,emailAl);
intentEmail.putExtra(Intent.EXTRA_SUBJECT , "Subject");
intentEmail.putExtra(Intent.EXTRA_TEXT , "");
}
} catch(IOException e){
e.printStackTrace();
}
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
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}};
// Assign the listener to that action item
MenuItemCompat.setOnActionExpandListener(share,el);
return super.onCreateOptionsMenu(menu);
}
答案 0 :(得分:0)
我相信您正在正确地编写文件,但是您传递给intent的内容不是文件本身而是父目录...还有几个优化提示,因为您在for中执行了大量不必要的代码可能在外面的循环...
File dest = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()); // Doesn't rely on any iterating information in the for cycle, move it outside..
for(String file : mp3Files ){
File filein = new File(file);
uri = Uri.fromFile(filein);
InputStream in = new FileInputStream(filein.getName());
File outFile = new File(dest, filein.getName()); // IMPORTANT! You need to create your file object separately, so you can then pass it to intent as well..
FileOutputStream 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);
}
//Pass your lists after the iterating has finished ...
intentShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM,al);
intentEmail.putParcelableArrayListExtra(Intent.EXTRA_STREAM,emailAl);
intentEmail.putExtra(Intent.EXTRA_SUBJECT , "Subject");
intentEmail.putExtra(Intent.EXTRA_TEXT , "");
!!!代码未测试!!! 如果这解决了您的问题,请告诉我们。