简体   繁体   中英

How to write data into an external storage android

Hello I am having a bit of trouble writing to a file , I have looked at this write here https://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage

But I don't seem to understand what I'm doing wrong every time I open up an email I sent to myself it says 0.0b next to the file and won't let me hear it or preview it . can anyone help me as to understanding why this code is the way it is and not working ????

    @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);
}

I believe that you are writing your files correctly, but you what you pass to the intent is not the file itself but rather a parent directory... There are also several optimization tips, as you do much unnecessary code in the for cycle which could be outside ...

       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 , "");

!!! Code not tested !!! Let us know if this resolved your problem ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM