簡體   English   中英

Android FilePicker對話框僅適用於某些設備

[英]Android FilePicker dialog only working for some devices

我正在使用常規樣板代碼在Android上顯示“文件選擇器”對話框。 獲取文件路徑后,我將使用aQuery將該文件上傳到服務器。 但是,該代碼僅能在運行於Android 4.1.2的舊三星手機上運行,​​而不能在任何其他設備上運行。 我使用許多設備對其進行了測試,但僅在具有JellyBean的Samsung設備上有效。

    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();
    }

onActivityResult代碼為:

    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);
}

我試圖編輯fPath.substring(7),沒有成功fpath,我不希望使用任何第三方庫。

提前致謝。

嘗試使用此

該庫可能會解決您的要求。

Data.getData()返回uri ; 你需要一個文件路徑; 嘗試找到將uri轉換為文件路徑的方法:可能:

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;  
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM