Android FilePicker dialog only working for some devices

995 Views Asked by At

I'm using the regular boiler-plate code for showing the File Picker dialog on android. After I get a file path, I'm uploading that file to my server using aQuery. However, the code is only working on my old samsung phone that runs on Android 4.1.2 but not on any other devices. I tested it using many devices but it works only on Samsung devices with JellyBean.

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType(minmeType);
    intent.addCategory(Intent.CATEGORY_OPENABLE);


    // special intent for Samsung file manager
    Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
    // if you want any file type, you can skip next line
    sIntent.putExtra("CONTENT_TYPE", minmeType);
    sIntent.addCategory(Intent.CATEGORY_DEFAULT);

    Intent chooserIntent;
    if (getPackageManager().resolveActivity(sIntent, 0) != null) {
        // it is device with samsung file manager
       chooserIntent = Intent.createChooser(sIntent, "Open file");
       chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{intent});
    } else {
        chooserIntent = Intent.createChooser(intent, "Open file");
    }

    try {
        startActivityForResult(chooserIntent, PICKFILE_REQUEST_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getApplicationContext(), "No suitable File Manager was found.\nPlease download a File Manager and try again.", Toast.LENGTH_LONG).show();
    }

The onActivityResult code is:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        fPath = data.getDataString();
        File file = new File(fPath.substring(7));
        params.put("file" + noFilesAttached, file);
        noFilesAttached = noFilesAttached + 1;
        path.setText(noFilesAttached + " files attached ");
        upload.setText("Upload (" + noFilesAttached + ")");
        upload.setVisibility(View.VISIBLE);
        isUploaded = false;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

I tried editing the fPath.substring(7) to fpath with no success, and I don't want to use any third-party libraries.

Thanks in advance.

2

There are 2 best solutions below

1
tiny sunlight On BEST ANSWER

Data.getData() return a uri; U need a filepath; Try to find way to transform uri to filepath: Possibly:

public static String getRealFilePath( final Context context, final Uri uri ) {  
    if ( null == uri ) return null;  
    final String scheme = uri.getScheme();  
    String data = null;  
    if ( scheme == null )  
        data = uri.getPath();  
    else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {  
        data = uri.getPath();  
    } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {  
        Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, null, null, null );  
        if ( null != cursor ) {  
            if ( cursor.moveToFirst() ) {  
                int index = cursor.getColumnIndex( ImageColumns.DATA );  
                if ( index > -1 ) {  
                    data = cursor.getString( index );  
                }  
            }  
            cursor.close();  
        }  
    }  
    return data;  
}  
1
AudioBubble On

Try using this library

This library might fix your requirement.