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