Android上从res文件到SD卡的文件

发布于 2024-11-06 13:19:11 字数 2300 浏览 2 评论 0原文

您好,我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-11-13 13:19:11

说明:

第一个 try/catch 块的作用是:

soundFile=null; 

如果读取失败。

然后,在第二个中,您使用声音文件:

FileInputStream fis = soundFile.createInputStream();

您正在读取 null,因为第一次尝试失败

编辑:

使用此代替

InputStream ins = context.getResources().openRawResource (R.raw.FILENAME)
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
String filename = Environment.getExternalStorageDirectory().toString()+File.separator+FILENAME;
FileOutputStream fos = new FileOutputStream(filename);
fos.write(buffer);
fos.close();

Explanation:

The first try/catch block does:

soundFile=null; 

if the read fails.

Then, in the second, you use soundfile in:

FileInputStream fis = soundFile.createInputStream();

You are reading null because the first try failed

Edit:

Use this instead

InputStream ins = context.getResources().openRawResource (R.raw.FILENAME)
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
String filename = Environment.getExternalStorageDirectory().toString()+File.separator+FILENAME;
FileOutputStream fos = new FileOutputStream(filename);
fos.write(buffer);
fos.close();
复古式 2024-11-13 13:19:11

您是否忘记在 Manifest 文件中设置写入权限?

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

Did you forget to set up permission to write in your Manifest file?

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文