I want to make an application that allows me to choose a file from SDcard (.apk file), then send it by HTTP Post to a web service. The problem is that even if I can choose the file, I can't convert it in a byte array because my application is looking for it in its private folders, not in SDcard.

I used Intent to access to FileManager and choose a file from storage. Then, a toast notification shows me its path.

public class MainActivity extends AppCompatActivity {    
    Button button1;
    Intent intent;
    String PathHolder;

    protected void onCreate(Bundle savedInstanceState) {

        button1 = findViewById(R.id.button1);    
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                intent = new Intent(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);    

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub    
        switch (requestCode) {    
            case 1:    
                if (resultCode == RESULT_OK) {    
                    PathHolder = data.getData().getPath();
                    File file = new File(PathHolder);

                    String FileName = file.getName();
                    Toast.makeText(MainActivity.this, PathHolder, Toast.LENGTH_LONG).show();

                    try {
                        byte[] bArray = new byte[(int) file.length()];
                        FileInputStream inputStream = new FileInputStream(file);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block


FileInputStream inputStream = new FileInputStream(f);

My application throws an Exception and the response is :

File Not Found

1 Answers

Noltibus On

The variable f, on which you are constructing the FileInputStream of is never assigned or am I wrong? I think you should change f to file.