45

i have a video file in my external directory. how can i load it to inputstream variable. For the time being i am reading file in the res/raw folder but i want to read it from the sdcard. also i dont know about the name of the file but its path will be recieved through intent. check the following code

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();
}}
Talha Malik
  • 1,509
  • 3
  • 22
  • 44
  • 1
    All three answers fail to understand what a URI is and that they aren't always a file path. If you had the file path you'd have figured it out yourself, but the posted answers just don't answer the actual question. – BooleanCheese May 13 '16 at 17:46

4 Answers4

91

Try

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

Then you can read from the stream.

Ben Jakuben
  • 3,147
  • 11
  • 62
  • 104
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • well thanks its working. but i cant here anything there is too much noise. Any idea why is it like this? – Talha Malik Mar 27 '13 at 07:48
  • 11
    Note: this answer only works for OP's question (OP have a uri that is a file path). In more general case, Uri.toString() does not necessarily returns a file path (uri could instead be a content path, or url, etc.). This method only works for Uri that is a file path. check answer to this question: http://stackoverflow.com/questions/5657411/android-getting-a-file-uri-from-a-content-uri – Helin Wang Oct 07 '14 at 00:34
  • 1
    Well the question is entitled "Load a file from external storage". I think assuming the incoming uri is a file is safe in that case. – Gabe Sechan Oct 14 '14 at 08:44
11
String fileName = "OfflineMap/maps.xml";
String path = Environment.getExternalStorageDirectory()+"/"+fileName;
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
DroidDev
  • 1,527
  • 4
  • 20
  • 42
Satya
  • 528
  • 1
  • 5
  • 9
3

Here is a working code, you can InputStream with a storage file:

       File sdcard = Environment.getExternalStorageDirectory();
       File file = new File(sdcard,"Demo.xml");
       InputStream fileInputStream = new FileInputStream(file);
Saleem Angel
  • 41
  • 1
  • 3
-3
soundFile = openFileInput(uri);
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Mobeen Altaf
  • 138
  • 1
  • 9