【问题标题】:How to create a File Chooser Dialog with Android Studio如何使用 Android Studio 创建文件选择器对话框
【发布时间】:2016-07-03 13:57:47
【问题描述】:

今天我想创建一个文件选择器,在选择文件后,它会打开一个带有所选文件路径的普通对话框。我试图做这个项目,但我不明白我该怎么做。

【问题讨论】:

    标签: android file dialog


    【解决方案1】:

    你可以看看这个:aFileChooser

    代码:

    清单中的活动

    <activity
    android:name="com.ipaulpro.afilechooser.FileChooserActivity"
    android:icon="@drawable/ic_chooser"
    android:enabled="@bool/use_activity"
    android:exported="true"
    android:label="@string/choose_file" >
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
    
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.OPENABLE" />
    
        <data android:mimeType="*/*" />
    </intent-filter>
    

    Java 代码

    private static final int REQUEST_CHOOSER = 1234;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // Create the ACTION_GET_CONTENT Intent
    Intent getContentIntent = FileUtils.createGetContentIntent();
    
    Intent intent = Intent.createChooser(getContentIntent, "Select a file");
    startActivityForResult(intent, REQUEST_CHOOSER);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent     data) {
    switch (requestCode) {
        case REQUEST_CHOOSER:   
            if (resultCode == RESULT_OK) {
    
                final Uri uri = data.getData();
    
                // Get the File path from the Uri
                String path = FileUtils.getPath(this, uri);
    
                // Alternatively, use FileUtils.getFile(Context, Uri)
                if (path != null && FileUtils.isLocal(path)) {
                    File file = new File(path);
                }
            }
            break;
     }
    }
    

    如果您想使用存储访问框架 (API 19+),请在您的 &lt;application&gt;: 中包含 Ian Lake 的 LocalStorageProvider(包含在此库中)

    <provider
        android:name="com.ianhanniballake.localstorage.LocalStorageProvider"
        android:authorities="com.ianhanniballake.localstorage.documents"
        android:enabled="@bool/use_provider"
        android:exported="true"
        android:grantUriPermissions="true"
        android:permission="android.permission.MANAGE_DOCUMENTS" >
            <intent-filter>
                <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
            </intent-filter>
    </provider>
    

    【讨论】:

    • 错误:(21, 35) 错误: 找不到符号变量 FileUtils
    • 它给了我 com.ianhanniballake.localstorage.LocalStorageProvider 和 android:enabled="@bool/use_provider" 的错误
    【解决方案2】:
    dependencies {
        compile 'org.apache.commons:commons-io:1.3.2'
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 2014-11-26
      • 1970-01-01
      相关资源
      最近更新 更多