问题描述
我将从原始文件夹复制音频文件到SD卡
的方法。它有两个输入端
I have a method that will copy an Audio file from raw folder to SD card
. It takes two inputs
- ressound:.OGG音频原始文件ID
- FNAME:在SD卡中的原始文件的文件名
更新
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;
}
如果该方法返回true,那么该文件是正确保存到SD卡。如果返回false,则存在错误。就我而言,该方法,当它进入 FileNotFoundException异常
块返回false。我找不到为什么!一切似乎都没什么问题。
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 ?
推荐答案
我的问题无关,与code。这是一个许可相关的事情。我加了权限应用程序标记
里面。它必须是在它之外。它必须是在根标签
。
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
!
谢谢您的回答!
这篇关于将原始文件到SD卡中的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!