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();
}}

  • Specifying Maven dependencies in build.gradle doesn't work
  • Android: Bitmap resizing using better resampling algorithm than bilinear (like Lanczos3)
  • Detect if Android SurfaceView is drawing/moving
  • I need to show an PDF in which text of pdf will auto fit to the screen width and remaining text move to the next line
  • Error text in TextInputLayout is covered by keyboard
  • Eclipse Android gen already exists but is not a source folder
  • Related posts:

    Running a native library on Android L. error: only position independent executables (PIE) are suppor...
    Android Views: can checking visibility before setting visibility improve performance?
    Adding extra images/icons to CardFragment (Android Wear)
    Gradle, Android and the ANDROID_HOME SDK location
    Parse with Facebook login - Possibly since APK v42 or Graph API v2.4
    How do I add custom data and fields to the the Contacts screen in Android?
  • Android layout folders: layout, layout-port, layout-land
  • Avoid android from shutting screen off while app (with Phonegap) is on
  • Send location updates to IntentService
  • Crash: java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$layout
  • How to create an AVD for Android 4.0.3? (Unable to find a 'userdata.img')
  • OkHTTP and Picasso don't run together
  • 3 Solutions collect form web for “Load a file from external storage to Inputstream”

    Try

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

    Then you can read from the stream.

    String fileName = "OfflineMap/maps.xml";
    String path = Environment.getExternalStorageDirectory()+"/"+fileName;
    File file = new File(path);
    FileInputStream fileInputStream = new FileInputStream(file);
    
    soundFile = openFileInput(uri);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.