41

我的外部目录中有一个视频文件。我怎样才能将它加载到输入流变量。目前我正在读取 res/raw 文件夹中的文件,但我想从 sdcard 中读取它。我也不知道文件的名称,但它的路径将通过意图接收。检查以下代码

public class SonicTest extends Activity
{
VideoView videoView;
String uri;
InputStream soundFile;
File file;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);                      
    setContentView(R.layout.main);

}

public void play(View view)
{
    new Thread(new Runnable() 
    {
        public void run()
        {
            float speed= (float) 1.0;
            float pitch= (float) 1.5;
            float rate= (float) 1.0;
            uri= Environment.getExternalStorageDirectory().toString();
            uri=uri+"/video.3gp";
            AndroidAudioDevice device = new AndroidAudioDevice(22050, 1);
            Sonic sonic = new Sonic(22050, 1);
            byte samples[] = new byte[4096];
            byte modifiedSamples[] = new byte[2048];
            InputStream soundFile = null;

    //soundFile = getContentResolver().openInputStream(Uri.parse(uri));
    soundFile=getResources().openRawResource(R.raw.video3);
    Log.i("testing","check if SoundFile is correct "+soundFile);

            int bytesRead;

            if(soundFile != null) {
                sonic.setSpeed(speed);
                sonic.setPitch(pitch);
                sonic.setRate(rate);
                do {
                    try {
                        bytesRead = soundFile.read(samples, 0, samples.length);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                    if(bytesRead > 0) {
                        sonic.putBytes(samples, bytesRead);
                    } else {
                        sonic.flush();
                    }
                    int available = sonic.availableBytes(); 
                    if(available > 0) {
                        if(modifiedSamples.length < available) {
                            modifiedSamples = new byte[available*2];
                        }
                        sonic.receiveBytes(modifiedSamples, available);
                        device.writeSamples(modifiedSamples, available);
                    }
                } while(bytesRead > 0);
                device.flush();
            }
        }
    } ).start();
}}
4

4 回答 4

86

尝试

File file = new File(Uri.toString());
FileInputStream fileInputStream = new FileInputStream(file);

然后你可以从流中读取。

于 2013-03-27T07:37:39.527 回答
11
String fileName = "OfflineMap/maps.xml";
String path = Environment.getExternalStorageDirectory()+"/"+fileName;
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
于 2014-10-14T14:01:18.747 回答
2

这是一个工作代码,您可以使用 InputStream 存储文件:

       File sdcard = Environment.getExternalStorageDirectory();
       File file = new File(sdcard,"Demo.xml");
       InputStream fileInputStream = new FileInputStream(file);
于 2020-04-26T04:49:08.080 回答
-3
soundFile = openFileInput(uri);
于 2013-03-27T07:54:28.117 回答