2016-10-12 3 views
0

안녕하세요 파일에 쓰는 약간의 문제가, 내가 여기 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

내가 제대로 파일을 작성하는 것으로 생각하지만, 당신이 의도를 전달할 것을 당신이 많은 불필요한처럼 여러 최적화 팁도있다 ... 파일 자체가 아니라 부모 디렉토리가 아닙니다 외부 될 수주기위한 코드 ...

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

! 테스트되지 않은 코드 !!! 문제가 해결 된 경우 저희에게 알려주십시오.

+0

새로 만든 파일은 어떻게 만들어야합니까? File mp3 = new File (filein.get name()); 하지만 그건 내 질문에 내가 통과해야합니까 그래서 작동하지 않았다 ???????? –

+0

내 대답에 게시 된 코드는 이미 다시 작성되었으므로 다음 행을 찾을 수 있습니다. 'File outFile = new File (dest, filein.getName());' – koperko

+0

위의 코드를 사용하여 해봤지만 여전히 그렇지 않습니다. 일. –

관련 문제