Intent.createChooser文件选择

实现点击Button选择文件, 在TextView上显示Uri

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.filetest.MainActivity" >
10 
11     <TextView
12         android:id="@+id/textView_uri"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/hello_world" />
16     <Button
17         android:id="@+id/btn_find"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:layout_below="@+id/textView_uri"
21         android:text="find"/>
22 
23 </RelativeLayout>
 1 package com.example.filetest;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.net.Uri;
 6 import android.os.Bundle;
 7 import android.view.Menu;
 8 import android.view.MenuItem;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 import android.widget.TextView;
13 
14 
15 public class MainActivity extends Activity {
16 
17     protected static final int FILE_SELECT_CODE = 0;
18     private Button button;
19     private TextView textView;
20     
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         
26         button = (Button)findViewById(R.id.btn_find);
27         textView = (TextView)findViewById(R.id.textView_uri);
28         
29         button.setOnClickListener(new OnClickListener() {
30             @Override
31             public void onClick(View v) {
32                 // TODO Auto-generated method stub
33                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
34                 intent.setType("*/*");  // 选择文件类型
35                 intent.addCategory(Intent.CATEGORY_OPENABLE);
36                 startActivityForResult( Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
37             }
38         });
39     }
40 
41     @Override
42     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
43         // TODO Auto-generated method stub
44         switch(requestCode){
45             case FILE_SELECT_CODE:
46                 if(resultCode == RESULT_OK){
47                     Uri uri = data.getData();
48                     String path = uri.getPath();
49                     textView.setText(path);
50                 }
51                 break;
52         }
53         super.onActivityResult(requestCode, resultCode, data);
54     }
55 }

截图

选择文件之后

 

转载于:https://www.cnblogs.com/iMirror/p/4030827.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中可以打开相册的 Intent 集合如下: 1. 打开图片选择器: ```java Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "Select Picture"), requestCode); ``` 2. 打开视频选择器: ```java Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI); intent.setType("video/*"); startActivityForResult(Intent.createChooser(intent, "Select Video"), requestCode); ``` 3. 打开音频选择器: ```java Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); intent.setType("audio/*"); startActivityForResult(Intent.createChooser(intent, "Select Audio"), requestCode); ``` 4. 打开文件选择器: ```java Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(Intent.createChooser(intent, "Select File"), requestCode); ``` 以上代码中,`requestCode` 是打开相册的请求码,用于在 `onActivityResult` 方法中获取相册返回的结果。 此外,需要在 `AndroidManifest.xml` 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 以上代码会打开一个系统相册选择器,可以让用户选择一个媒体文件文件。用户选择完毕后,可以在 `onActivityResult` 方法中获取选中的文件的 Uri。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值