Android上从res文件到SD卡的文件
您好,我正在尝试将 Android 应用程序的 res/raw 文件夹中的文件复制到手机的 SD 卡上,以便将它们设置为铃声。这是我迄今为止在我的应用程序中得到的:
package com.wochstudios.ringtonetest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.*;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.res.*;
import android.app.Activity;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class setRingtone extends Activity {
Button setBtn;
TextView result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setBtn = (Button) findViewById(R.id.button1);
result = (TextView) findViewById(R.id.textView1);
setBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Attempt to make file out on raw element
setRing();
}
});
}
public void setRing(){
File newSoundFile = new File("/sdcard/media/ringtone", "fortkickass.mp3");
Uri mUri = Uri.parse("android.resource://com.wochstudios.ringtonetest/R.raw.fortkickass");
ContentResolver mCr = getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile=null;
}
try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
}
}
一旦遇到 setRing 方法的第二次尝试捕获,就会崩溃,我从之前发布的问题中得到了这段代码,但我再也找不到链接了,任何帮助都会很棒
编辑
我闲逛了一下,发现 Assetfiledescriptor soundFile 为空,所以是什么导致了崩溃,所以现在我需要弄清楚为什么它为空。
Hi I'm trying to get files in my res/raw folder of my android application to be copied onto the phone's sdcard so they can be set as ringtones. Here is what I have so far on my application:
package com.wochstudios.ringtonetest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.*;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.res.*;
import android.app.Activity;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class setRingtone extends Activity {
Button setBtn;
TextView result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setBtn = (Button) findViewById(R.id.button1);
result = (TextView) findViewById(R.id.textView1);
setBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Attempt to make file out on raw element
setRing();
}
});
}
public void setRing(){
File newSoundFile = new File("/sdcard/media/ringtone", "fortkickass.mp3");
Uri mUri = Uri.parse("android.resource://com.wochstudios.ringtonetest/R.raw.fortkickass");
ContentResolver mCr = getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile= mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile=null;
}
try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
}
}
this crashes once it hits the second try catch of the setRing method, i got this code from a question posted earlier but i can't find the link anymore any help would be great
EDIT
i fooled around and discovered that the Assetfiledescriptor soundFile is null so that what causes the crash so now i need to figure out why it is null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
说明:
第一个 try/catch 块的作用是:
如果读取失败。
然后,在第二个中,您使用声音文件:
您正在读取 null,因为第一次尝试失败
编辑:
使用此代替
Explanation:
The first try/catch block does:
if the read fails.
Then, in the second, you use soundfile in:
You are reading null because the first try failed
Edit:
Use this instead
您是否忘记在 Manifest 文件中设置写入权限?
Did you forget to set up permission to write in your Manifest file?