Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a file from external storage to Inputstream

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(); }} 
like image 832
Talha Malik Avatar asked Mar 27 '13 07:03

Talha Malik


People also ask

What is the difference between InputStream and FileInputStream?

There is no real difference. FileInputStream extends InputStream , and so you can assign an InputStream object to be a FileInputStream object. In the end, it's the same object, so the same operations will happen. This behavior is called Polymorphism and is very important in Object-Oriented Programming.

What is InputStream from a file?

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader . Since: JDK1.0. See Also: File , FileDescriptor , FileOutputStream , Files.


2 Answers

Try

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

Then you can read from the stream.

like image 104
Gabe Sechan Avatar answered Sep 30 '22 06:09

Gabe Sechan


String fileName = "OfflineMap/maps.xml"; String path = Environment.getExternalStorageDirectory()+"/"+fileName; File file = new File(path); FileInputStream fileInputStream = new FileInputStream(file); 
like image 43
Satya Avatar answered Sep 30 '22 07:09

Satya