Move Raw file to SD card in Android

一曲冷凌霜 提交于 2019-12-31 05:42:27

问题


I have a method that will copy an Audio file from raw folder to SD card. It takes two inputs

  • ressound: .ogg Audio Raw file id.
  • fName: the file name of the raw file in the SD card.

Updated

     public boolean saveas(int ressound, String fName){  
     byte[] buffer=null;  
     InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
     int size=0;  

     try {  
      size = fIn.available();  
      buffer = new byte[size];  
      fIn.read(buffer);  
      fIn.close();  
     } catch (IOException e) {  
      // TODO Auto-generated catch block 
      return false;  
     }  

     String path="/sdcard/music/[my_package_name]/"; 
     String filename=fName+".ogg";
     String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
     String completePath = baseDir + File.separator + "music" + File.separator + "my_package" + File.separator + filename;


     boolean exists = (new File(completePath)).exists();  
     if (!exists){new File(completePath).mkdirs();}  

     FileOutputStream save;  
     try {  
      save = new FileOutputStream(completePath);  
      save.write(buffer);  
      save.flush();  
      save.close();  
     } catch (FileNotFoundException e) {  
      // TODO Auto-generated catch block  
      return false;
     } catch (IOException e) {  
      // TODO Auto-generated catch block 
      return false;  
     }      

     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

     File k = new File(path, filename);  

     ContentValues values = new ContentValues();  
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
     values.put(MediaStore.MediaColumns.TITLE, "exampletitle");  
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
     values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");  
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
     values.put(MediaStore.Audio.Media.IS_ALARM, true);  
     values.put(MediaStore.Audio.Media.IS_MUSIC, true);  

     //Insert it into the database  
     this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  

     return true;  
    } 

If the method returns true, Then the file is saved correctly to SD card. If it returns false, then there is an error. In my case, The method returns false when it enters the FileNotFoundException block. I can't find why !! everything seems fine to me.

In case you ask me if I add the WRITE permission, I added it to the application tag in the Manifest file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Any help please ?


回答1:


Use this for path:

string baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
string path = baseDir + File.Separator + "music" + File.Separator + "yourpackagename" + filename;



回答2:


Please don't hardcode paths. Use Environment.getExternalStorageDirectory() to get the path of a large storage medium. Some phones mount sdcards in /mnt/sdcard or somewhere else, some just don't have one and rely on inbuilt flash.

This might throw the error, but I don't think is the error in this case since you are probably testing with your phone/emulator, where this path applies.




回答3:


My problem has nothing to do with the code. It is a permission-related thing. I added the permission inside the application tag. It must be outside it. It must be in the root tag.

Wired for me because it make sense to put it in the application tag not the Manifest tag !

Thanks for your answers !



来源:https://stackoverflow.com/questions/6979521/move-raw-file-to-sd-card-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!