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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent, 1);    
            }
        });
    }

    @Override
    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);
                        inputStream.read(bArray);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
        }
    }
}

After

FileInputStream inputStream = new FileInputStream(f);

My application throws an Exception and the response is :

File Not Found

1 Answers

0
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.