我正在尝试编写一个 Android 应用程序,该应用程序需要查询用户以选择一些数据文件所在的目录。
执行以下代码时,会启动 Android 文件选择器并选择一个目录。
返回的 URI 对我来说看起来有效:
content://com.android.externalstorage.documents/tree/primary%3ANotifications
然后,当我查询内容提供者时,它崩溃了。
返回的错误是这样的:
Caused by: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.externalstorage.documents/tree/primary%3ANotifications
这是我的代码:
package com.muddco.fptest2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
static String TAG = "TEST123";
public static TextView textView, uriView;
public Object act = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.view1);
uriView = (TextView) findViewById(R.id.uriview);;
showFileChooser();
}
private static final int FILE_SELECT_CODE = 0;
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addCategory(Intent.CATEGORY_DEFAULT);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a Directory"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
@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();
Log.d(TAG, "File Uri: " + uri.toString());
uriView.setText("URI: "+ uri.toString());
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(uri, takeFlags);
ContentResolver contentResolver = getContentResolver();
String[] projection = { "*" };
Cursor cursor = contentResolver.query(uri,projection,null,null, null );
// We never get here - the query crashes with an Unsuported URI content error
if(cursor!=null) {
TextView pathView = (TextView) findViewById(R.id.pathView);
pathView.setText("URI: ");
cursor.moveToFirst();
// Loop in the cursor to get each row.
do {
// Get column 1 value.
int column1Index = cursor.getColumnIndex("column1");
String column1Value = cursor.getString(column1Index);
// Get column 2 value.
int column2Index = cursor.getColumnIndex("column2");
String column2Value = cursor.getString(column2Index);
pathView.setText(pathView.getText() + column1Value + " | " + column2Value + "\n");
} while (cursor.moveToNext());
}
//Toast.makeText(this, "File Uri: " + uri.toString(),
// Toast.LENGTH_LONG).show();
// Get the path
// Get the file instance
// File file = new File(path);
// Initiate the upload
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
我在 list 中设置了权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
请原谅我糟糕的编码,因为我刚刚学习 Android 编程。
任何有关如何使 thw 正常工作的建议将不胜感激。
谢谢!!!
最佳答案
感谢使用 CommonsWare,我已经设法让事情正常进行。
这是工作代码:
package com.muddco.fl99;
import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;;
import androidx.documentfile.provider.DocumentFile;
public class MainActivity extends AppCompatActivity {
static String TAG = "TEST123";
public static TextView fileView, uriView;
public Context act = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileView = (TextView) findViewById(R.id.fileView);
uriView = (TextView) findViewById(R.id.uriView);;
showFileChooser();
}
private static final int FILE_SELECT_CODE = 0;
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addCategory(Intent.CATEGORY_DEFAULT);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a Directory"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
@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();
Log.d(TAG, "File Uri: " + uri.toString());
uriView.setText("URI: "+ uri.toString());
DocumentFile dfile = DocumentFile.fromTreeUri(act, uri);
DocumentFile[] fileList = dfile.listFiles();
Log.d(TAG,fileList.toString() );
int jj=1;
for (DocumentFile docfile : fileList) {
Log.d(TAG, "File: " + docfile.getUri() + "\n");
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
关于java - 为什么我的应用会发出 Unsupported URI content 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59849225/