通過文件管理器器選擇文件獲取文件地址。

package com.mytest.filemanger;

import android.app.*;

import android.content.*;

import android.database.*;

import android.net.*;

import android.os.*;

import android.provider.*;

import android.support.v7.app.*;

import android.view.*;

import android.widget.*;

public class MainActivity extends AppCompatActivity {?

? ? @Override?

? ? protected void onCreate(Bundle savedInstanceState) {?

? ? ? ? super.onCreate(savedInstanceState);?

? ? ? ? setContentView(R.layout.main);?

? ? ? ? Button btn= (Button) findViewById(R.id.btn);?

? ? ? ?

? ? ? ? btn.setOnClickListener(new View.OnClickListener() {?

? ? ? ? ? ? ? ? @Override?

? ? ? ? ? ? ? ? public void onClick(View v) {?

? ? ? ? ? ? ? ? chooseFile();

? ? ? ? ? ? ? ? ? ? Intent intent = new Intent(Intent.ACTION_GET_CONTENT);?

? ? ? ? ? ? ? ? ? ? intent.setType("text/xls");//設(shè)置類型,我這里是任意類型,任意后綴的可以這樣寫。?

? ? ? ? ? ? ? ? ? ? //intent.setType(“audio/*”)

? ? ? ? ? ? ? ? ? ? //intent.setType(“image/*”);//選擇圖片?

//intent.setType(“audio/*”); //選擇音頻?

//intent.setType(“video/*”); //選擇視頻 (mp4 3gp 是android支持的視頻格式)?

//intent.setType(“video/*;image/*”);//同時(shí)選擇視頻和圖片

? ? ? ? ? ? ? ? ? ? intent.addCategory(Intent.CATEGORY_OPENABLE);?

? ? ? ? ? ? ? ? ? ? startActivityForResult(intent,1);?

? ? ? ? ? ? ? ? }?

? ? ? ? ? ? });?

? ? }?

? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {?

? ? ? ? TextView textview=(TextView)findViewById(R.id.fileurl);

? ? if (resultCode == Activity.RESULT_OK) {?

? ? ? ? ? ? if (requestCode == 1) {?

? ? ? ? ? ? ? ? Uri uri = data.getData();?

? ? ? ? ? ?

? ? ? ? ? ? textview.setText(getFilePathFromUri(this, uri) );

? ? ? ? ? ? ? ? //Toast.makeText(this, "文件路徑:"+uri.getPath().toString(), Toast.LENGTH_LONG).show();?

? ? ? ? ? ? }?

? ? ? ? }?

? ? }

? ? private static final int FILE_SELECT_CODE = 0;?

? ? private static final String TAG = "VideoActivity";?

? ? private void chooseFile() {?

? ? ? ? Intent intent = new Intent(Intent.ACTION_GET_CONTENT);?

? ? ? ? intent.setType("text/xls");?

? ? ? ? intent.addCategory(Intent.CATEGORY_OPENABLE);?

? ? ? ? try {?

? ? ? ? ? ? startActivityForResult(Intent.createChooser(intent, "選擇文件"), FILE_SELECT_CODE);?

? ? ? ? } catch (android.content.ActivityNotFoundException ex) {?

? ? ? ? ? ? Toast.makeText(this, "親,木有文件管理器啊-_-!!", Toast.LENGTH_SHORT).show();?

? ? ? ? }?

? ? }?

? ?

? ? public static String getFilePathFromUri(Context context, 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[]{MediaStore.Images.ImageColumns

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .DATA}, null, null, null);

? ? ? ? ? ? if (null != cursor) {

? ? ? ? ? ? ? ? if (cursor.moveToFirst()) {

? ? ? ? ? ? ? ? ? ? int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

? ? ? ? ? ? ? ? ? ? if (index > -1) {

? ? ? ? ? ? ? ? ? ? ? ? data = cursor.getString(index);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? cursor.close();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return data;

? ?

? ? }

?

? ?

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容