Android फ़ाइल चयनकर्ता [बंद]


115

मैं एक फाइल अपलोडर बनाना चाहता हूं। और इसलिए मुझे एक फ़ाइल चयनकर्ता की आवश्यकता है लेकिन मैं इसे स्वयं नहीं लिखना चाहता। मुझे ओआई फाइल मैनेजर लगता है और मुझे लगता है कि यह मुझे सूट करता है। लेकिन मैं उपयोगकर्ता को OI फ़ाइल प्रबंधक स्थापित करने के लिए कैसे मजबूर कर सकता हूं? यदि मैं नहीं कर सकता, तो क्या मेरे ऐप में फ़ाइल प्रबंधक को शामिल करने का एक बेहतर तरीका है? धन्यवाद




जवाबों:


267

EDIT ( 02 जनवरी 2012 ):

मैंने एक छोटा सा ओपन सोर्स एंड्रॉइड लाइब्रेरी प्रोजेक्ट बनाया , जो इस प्रक्रिया को सुव्यवस्थित करता है, जबकि एक बिल्ट-इन फ़ाइल एक्सप्लोरर भी उपलब्ध कराता है (यदि उपयोगकर्ता के पास एक भी मौजूद नहीं है)। यह केवल कोड की कुछ पंक्तियों की आवश्यकता के उपयोग के लिए बेहद सरल है।

आप इसे GitHub: aFileChooser पर पा सकते हैं ।


मूल

यदि आप चाहते हैं कि उपयोगकर्ता सिस्टम में किसी भी फ़ाइल को चुनने में सक्षम हो, तो आपको अपना स्वयं का फ़ाइल प्रबंधक शामिल करना होगा, या उपयोगकर्ता को एक डाउनलोड करने की सलाह देना होगा। मेरा मानना ​​है कि आप जो सबसे अच्छा कर सकते हैं वह Intent.createChooser()इस तरह से "ओपन करने योग्य" सामग्री के लिए है :

private static final int FILE_SELECT_CODE = 0;

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.", 
                Toast.LENGTH_SHORT).show();
    }
}

फिर आप चयनित फ़ाइल के लिए है सुनना Uriमें onActivityResult()तो जैसे:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file 
            Uri uri = data.getData();
            Log.d(TAG, "File Uri: " + uri.toString());
            // Get the path
            String path = FileUtils.getPath(this, uri);
            Log.d(TAG, "File Path: " + path);
            // Get the file instance
            // File file = new File(path);
            // Initiate the upload
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

getPath()मेरे में विधि FileUtils.javaहै:

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

1
मैं तो यहाँ तक जाऊँगा कि उन्हें भी बाजार में जाने के लिए निर्देशित करूँ।
कुर्तिस नुसबूम

2
लेकिन मैं FileUtils नहीं ढूँढ सकता ....
भालू

2
@Bicou नोट के लिए धन्यवाद। आपकी कुहनी ने मुझे आलसी होने से रोकने और कुछ मामूली बदलाव करने में मदद की। :-) मैंने सिर्फ लायब्रेरी के लिए एक अद्यतन को शामिल किया है जिसमें लाइसेंस शामिल है।
पॉल बर्क

22
यह उत्तर इस तरह से uri पर विचार नहीं करता है: "सामग्री: //com.android.providers.media.documents/document/image: 62"।
वांग्कि ०६०

2
@ wangqi060934: आपने ऐसे यूरी के साथ काम करने का प्रबंधन कैसे किया? कृपया कार्यक्षमता प्राप्त करने के लिए अपने अनुभव को साझा करें
मेहुल जोसर

-3

मैंने इस उद्देश्य के लिए AndExplorer का उपयोग किया और मेरा समाधान एक डायलॉग पॉपअप है और फिर बाजार पर अनुप्रेषण को मिसिंग एप्लिकेशन इंस्टॉल करने के लिए है:

मेरा प्रारंभ बाहरी फ़ाइल / निर्देशिका पिकर को कॉल करने का प्रयास कर रहा है। अगर यह कॉल शो को गायब कर रहा है installResultMessage फ़ंक्शन।

private void startCreation(){
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_PICK);
    Uri startDir = Uri.fromFile(new File("/sdcard"));

    intent.setDataAndType(startDir,
            "vnd.android.cursor.dir/lysesoft.andexplorer.file");
    intent.putExtra("browser_filter_extension_whitelist", "*.csv");
    intent.putExtra("explorer_title", getText(R.string.andex_file_selection_title));
    intent.putExtra("browser_title_background_color",
            getText(R.string.browser_title_background_color));
    intent.putExtra("browser_title_foreground_color",
            getText(R.string.browser_title_foreground_color));
    intent.putExtra("browser_list_background_color",
            getText(R.string.browser_list_background_color));
    intent.putExtra("browser_list_fontscale", "120%");
    intent.putExtra("browser_list_layout", "2");

    try{
         ApplicationInfo info = getPackageManager()
                                 .getApplicationInfo("lysesoft.andexplorer", 0 );

            startActivityForResult(intent, PICK_REQUEST_CODE);
    } catch( PackageManager.NameNotFoundException e ){
        showInstallResultMessage(R.string.error_install_andexplorer);
    } catch (Exception e) {
        Log.w(TAG, e.getMessage());
    }
}

यह मिथोस केवल एक संवाद है और यदि उपयोगकर्ता बाज़ार से बाहरी एप्लिकेशन इंस्टॉल करना चाहता है

private void showInstallResultMessage(int msg_id) {
    AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.setMessage(getText(msg_id));
    dialog.setButton(getText(R.string.button_ok),
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
    dialog.setButton2(getText(R.string.button_install),
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse("market://details?id=lysesoft.andexplorer"));
                    startActivity(intent);
                    finish();
                }
            });
    dialog.show();
}

तो, अपने एप्लिकेशन उपयोगकर्ताओं के लिए आवश्यकताओं में से एक है ... स्थापित करें AndExplorer ?!
फैंटमैक्सएक्स
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.