[英]How to write data into an external storage android
您好,我在写入文件时遇到了麻烦,我在这里查看了此写入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);
}
我相信您正在正确地编写文件,但是您传递给该意图的不是文件本身,而是父目录...还有一些优化技巧,因为您在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 , "");
!!! 代码未经测试!!! 让我们知道这是否解决了您的问题..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.