Android 文件選擇的實現(xiàn)代碼
更新時間:2013年08月19日 15:43:48 作者:
這篇文章介紹了Android 文件選擇的實現(xiàn)代碼,有需要的朋友可以參考一下
打開文件選擇器
復(fù)制代碼 代碼如下:
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) {
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
選擇的結(jié)果
復(fù)制代碼 代碼如下:
@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();
String path = FileUtils.getPath(this, uri);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
FileUtils文件
復(fù)制代碼 代碼如下:
public class FileUtils {
public static String getPath(Context context, Uri uri) {
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;
}
}
這個很簡單。
出處:http://www.cnblogs.com/linlf03/
相關(guān)文章
android nfc常用標(biāo)簽讀取總結(jié)
NFC(Near Field Communication,近場通信)是一種數(shù)據(jù)傳輸技術(shù)這篇文章主要介紹了android nfc常用標(biāo)簽讀取總結(jié),有興趣的可以了解一下。2016-12-12Android編程實現(xiàn)應(yīng)用程序開機自啟動的方法
這篇文章主要介紹了Android編程實現(xiàn)應(yīng)用程序開機自啟動的方法,涉及Android權(quán)限控制及廣播操作相關(guān)技巧,需要的朋友可以參考下2017-02-02Android拼圖游戲 玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化
這篇文章主要介紹了Android拼圖游戲的實現(xiàn)方法,教大家玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10