1

I'm attempting to load a file from my hard drive to a file input stream using the code below.

package com.filefinder1;

import java.io.File;
import java.io.FileInputStream;
import android.app.Activity;
import android.os.Bundle;

public class FileFinder_1Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try
        {
        String pathToFile = "C:\\\\Koala_Small.jpg";
        System.out.println("File Path: "+pathToFile);
        File file = new File(pathToFile);
         FileInputStream fileInputStream = new FileInputStream(file);
    }
    catch (Exception ex)
    {
    System.out.println("Error Catch Triggered: "+ex);
    }
    }
}

I've tried a bunch of different variations on the file path but nothing everything returns the following error:

01-10 10:59:06.189: I/System.out(2218): Error Catch Triggered: java.io.FileNotFoundException: /C:\\Koala_Small.jpg (No such file or directory)

Some where along the way an extra "/" seems to be getting added to the file path (not sure why). When I ask the system to print the file path prior to trying to load it into a file variable it returns:

01-10 10:59:06.189: I/System.out(2218): File Path: C:\\Koala_Small.jpg

I have tried a bunch of different variations of the file path (lowercase "c", two slashes instead of four...) nothing seems to fix the problem. Does any one have any idea what might be going wrong here?

2
  • 1
    Your phone/emulator should not have access to your harddrive... Commented Jan 10, 2012 at 19:06
  • 1
    C:\\\\Koala_Small.jpg? That's a ton of slashes...also try using a single forward slash. C:/ for instance. Commented Jan 10, 2012 at 19:07

2 Answers 2

5

You should put the file in your assets directory in your android project.

Then execute the following from your activity:

getAssets().open("Koala_Small.jpg");

This will return your input stream.

As it has been mentioned in the comments the emulator doesn't have access to your pc file system in the way you are trying to do it.

Sign up to request clarification or add additional context in comments.

Comments

0

place the file in your emulator drive and please use only 2 slashes for the path

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.