gpt4 book ai didi

java - Android 馅饼 : I cant get the file name or the file path

转载 作者:行者123 更新时间:2023-12-02 10:21:00 26 4
gpt4 key购买 nike

我的代码在任何设备上都可以正常工作..但不能在 android 派上工作

我正在尝试从 fragment 中的 onActivityResult 获取真实路径和文件名

我正在使用 apachi 库中的 FileNameUtils

并使用这个库

https://gist.github.com/tatocaster/32aad15f6e0c50311626

但它给了我 null

这是我的代码

private void pickFile() {

Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), PICKFILE_REQUEST_CODE);

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICKFILE_REQUEST_CODE) {

filePath = RealPathUtil.getRealPath(getContext(), data.getData());
fileName = FilenameUtils.getName(filePath);

Log.i("FileNameIs",filePath + "Hello " + fileName );

// if (fileName !=null)
// {
// mFileName
// .setText(fileName.isEmpty() ? getString(R.string.failed_please_try_again) : fileName);
//
// deleteOldPath();
//
// }
}
}

更新了..在下一个版本中修复了它..

第一个

获取文件名

我用过这个功能

  public void dumpImageMetaData(Uri uri) {

String TAG = "TagIs";
// The query, since it only applies to a single document, will only return
// one row. There's no need to filter, sort, or select fields, since we want
// all fields for one document.
Cursor cursor = getActivity().getContentResolver()
.query(uri, null, null, null, null, null);

try {
// moveToFirst() returns false if the cursor has 0 rows. Very handy for
// "if there's anything to look at, look at it" conditionals.
if (cursor != null && cursor.moveToFirst()) {

// Note it's called "Display Name". This is
// provider-specific, and might not necessarily be the file name.
String displayName = cursor.getString(
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
fileName = displayName ==null ? "Failed" : displayName;

deleteOldPath();

}
} finally {
cursor.close();
}
}

然后我使用了这篇文章中的方法

Get Real Path For Uri Android

获取文件路径

public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
Log.i("URI",uri+"");
String result = uri+"";
// DocumentProvider
// if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if (isKitKat && (result.contains("media.documents"))) {
String[] ary = result.split("/");
int length = ary.length;
String imgary = ary[length-1];
final String[] dat = imgary.split("%3A");
final String docId = dat[1];
final String type = dat[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
} else if ("audio".equals(type)) {
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
dat[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}

最佳答案

看看这个。 android reference

关于java - Android 馅饼 : I cant get the file name or the file path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54341476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com