android 选择文件获取路径吗,如何从android中的外部存储获取文件路径?

我在挑选文件的文件路径时遇到了问题,我搜索了所有堆栈溢出,但问题没有解决。从设备中选择文件的代码如下所示。如何从android中的外部存储获取文件路径?

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType("*/*");

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

//intent.addFlags(ST)

startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);

从意图拾取文件由

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

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK) {

if (requestCode == PICK_FILE_REQUEST) {

if (data != null) {

//no data present

Uri uri = data.getData();

String filePath = data.getData().getPath();

// String path = uri.getPath();

file = new File(filePath);

String name = getContentName(getContentResolver(), uri);

try {

InputStream inStream = getContentResolver().openInputStream(uri);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

try {

bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

} catch (IOException e) {

e.printStackTrace();

}

LinearLayout linearLayout = new LinearLayout(this);

linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,

LinearLayout.LayoutParams.WRAP_CONTENT));

linearLayout.setOrientation(LinearLayout.VERTICAL);

ImageView imageView = new ImageView(this);

imageView.setImageBitmap(bitmap);

attachFile.addView(imageView);

TextView textView = new TextView(this);

textView.setText(name);

attachFile.addView(textView);

return;

}

}

}

}

在上面的代码文件路径获得由`字符串文件路径= data.getData()获得的。的getPath() ;但是当上传文件到服务器异常被抛出像无效的文件和文件。如何从uri获取文件的正确路径?

但文件的名称是通过使用

`public static String getContentName(ContentResolver resolver, Uri uri) {

Cursor cursor = resolver.query(uri, null, null, null, null);

cursor.moveToFirst();

int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);

if (nameIndex >= 0) {

return cursor.getString(nameIndex);

} else {

return null;

}

}`

如何获得拾取文件正确的文件路径挑?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值