URI从Intent.ACTION_GET_CONTENT到文件 [英] URI from Intent.ACTION_GET_CONTENT into File

查看:311
本文介绍了URI从Intent.ACTION_GET_CONTENT到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 在使用Intent.ACTION_GET_CONTENT启动照片选择器
  2. 检索所选项目的URI
  3. 检索URI路径,这样我可以张贴到我的网络服务器

  1. Launch photo picker using Intent.ACTION_GET_CONTENT
  2. Retrieve URI of selected item
  3. Retrieve PATH of URI so that I can POST it to my webserver

code,推出浏览

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, BROWSE_IMAGE_REQUEST_CODE);

code检索选定的图片

if (RESULT_OK == resultCode &&
             BROWSE_IMAGE_REQUEST_CODE == requestCode) {
Uri uri = data.getData();

code发送到Web服务器

File file = new File(uri.getPath());
new FileSystemResourceFile(file);

我目前能检索的URI的路径没有概率 /外部/图片/媒体/ 24 但出于某种奇怪的原因,文件总是空,帮助吗?

I am currently able to retrieve the PATH from the URI no prob /external/images/media/24 but for some weird reason file is always null, help please?

推荐答案

我做这个方法转换乌里 Intent.ACTION_GET_CONTENT 来真正的路径:

I've done this method to convert Uri from Intent.ACTION_GET_CONTENT to real path:

public static String getRealPathFromUri(Activity activity, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

而这又转化成文件

Uri filePathFromActivity = (Uri) extras.get(Intent.EXTRA_STREAM);
filePathFromActivity = Uri.parse(FileUtil.getRealPathFromUri( (Activity) IntentActivity.this, filePathFromActivity));
File imageFile = new File(filePathFromActivity.getPath());

这篇关于URI从Intent.ACTION_GET_CONTENT到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆