Android 文件選擇的實(shí)現(xiàn)代碼
更新時(shí)間:2013年08月19日 15:43:48 作者:
這篇文章介紹了Android 文件選擇的實(shí)現(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;
}
}
這個(gè)很簡單。
出處:http://www.cnblogs.com/linlf03/
相關(guān)文章
Android SwipeMenuListView框架詳解分析
這篇文章主要介紹了Android SwipeMenuListView框架詳解分析的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)手機(jī)震動抖動效果的方法
今天小編就為大家分享一篇關(guān)于Android實(shí)現(xiàn)手機(jī)震動抖動效果的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
Android自定義ImageView實(shí)現(xiàn)圓角功能
這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實(shí)現(xiàn)圓角功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
如果你在Android Studio碰到gradle的各種問題就來看這篇文章吧(強(qiáng)烈建議收藏)
這篇文章主要介紹了你可能會在Android Studio碰到gradle的各種問題,完美解決關(guān)于gradle的全部問題,切記收藏以防需要的時(shí)候找不到了哦2021-08-08
android控件Banner實(shí)現(xiàn)簡單輪播圖效果
這篇文章主要為大家詳細(xì)介紹了android控件Banner實(shí)現(xiàn)簡單輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
解決Android應(yīng)用冷啟動時(shí)出現(xiàn)的白屏問題的方法
本篇文章主要介紹了解決Android應(yīng)用冷啟動時(shí)出現(xiàn)的白屏問題的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Flutter?Flow實(shí)現(xiàn)滑動顯隱層示例詳解
這篇文章主要為大家介紹了Flutter?Flow實(shí)現(xiàn)滑動顯隱層示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法示例
這篇文章主要介紹了Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法,涉及Android針對button按鈕的事件響應(yīng)及TextView動態(tài)添加相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

