get file location taking uri

0

I have implemented the following code to locate a file, and everything works fine until I select with the selector that brings the Android, which then returns null.

public void selectFile() {
    Intent chooseFile;
    Intent intent;
    chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
    chooseFile.setType("*/*");
    chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
    intent = Intent.createChooser(chooseFile, getString(R.string.select_file));
    startActivityForResult(intent, Def.ACTIVITY_CHOOSE_FILE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK){
        String p = getPath(this, data.getData());
        if (p!= null && !p.isEmpty())
            Toast.makeText(this, path, Toast.LENGTH_LONG).show();
        else
            Toast.makeText(this, "Error al encontrar el archivo",Toast.LENGTH_LONG).show();
    }
}

public static String getPath(Context context, Uri uri) {
    String result = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        try {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{ "_data" }, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                result= cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        result = uri.getPath();
    }

    return result;

}

EDIT: Sorry, I had the error

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/audio:1204 flg=0x1 }} to activity {org.sanjuan.dev/org.sanjuan.Acty}: java.lang.NullPointerException: println needs a message
    
asked by San Juan 11.07.2017 в 18:01
source

0 answers