0

I have an app that opens up a file picker and it outputs the path of that file in a Toast Message. But I would like to change it such that the file that I pick is passed as a parameter to a function.

My activity looks like this:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.btn_picker);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            startActivityForResult(intent, 7);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    switch (requestCode) {
        case 7:
            if (resultCode == RESULT_OK) {
                String PathHolder = data.getData().getPath();
                Toast.makeText(MainActivity.this, "The Files path is: "+ PathHolder, Toast.LENGTH_LONG).show();
            }
            break;
    }
}
}

And it does what it is supposed to do but instead of outputting the file path, I would like to call the function

 importToFile()

From the manage class.

I would like to do something like this:

manage.importToFile(File1)

Where File1 is the file I selected from the file picker.

How can I do that.

Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Koos
  • 117
  • 4
  • 15

1 Answers1

0

You can use File class in android to work with files. Create an instance of File using the path of the selected file and pass it to that method of yours. Something like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    switch (requestCode) {
        case 7:
            if (resultCode == RESULT_OK) {
                // as @Taseer Ahmad said its not a good way to get path of the file.
                //String PathHolder = data.getData().getPath();
                String PathHolder = getPath(this, data.getData());
                if (!TextUtils.isEmpty(PathHolder)) {
                    File file = new File(PathHolder);
                    manager.importToFile(file);
                    Toast.makeText(MainActivity.this, "The Files path is: "+ PathHolder, Toast.LENGTH_LONG).show();
                }
            }
            break;
    }
}

EDIT
As @Taseer Ahmad said, data.getData().getPath() is not a safe way to get path of the selected file. This code is copied from here.

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
} 
Amin Mousavi
  • 1,220
  • 10
  • 21
  • 2
    `getPath()` will fail if the `Uri` is a `content://` type or non-Uri type. A better approach would be to use create a 'generic' file, gets its URI from `FileProvider.getUriFromFile()`, open I/O streams and then start the copy function. `InputStream` will contain the `Uri` fetched from file picker. – Taseer Jun 03 '19 at 08:14