小编Mic*_*neš的帖子

android:从uri获取真正的路径

我想将文件发送到服务器,我需要从uri获得真正的路径.

我的代码:

public String getPathFromURI(Context context, Uri contentUri) {
    if ( contentUri.toString().indexOf("file:///") > -1 ){
        return contentUri.getPath();
    }

    Cursor cursor = null;
    try { 
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }finally {
          if (cursor != null) {
              cursor.close();
          }
    }
}
Run Code Online (Sandbox Code Playgroud)

和onActivityResult:

...
imageName = data.getData();
imagePath = getPathFromURI(getBaseContext(),imageName);

Picasso.with(getBaseContext()).load(imageName).into(imageView);
...
Run Code Online (Sandbox Code Playgroud)

如何在ImageView中显示图像,但imagePath总是为空?:) 谢谢

编辑:

我如何将图像发送到服务器

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost …
Run Code Online (Sandbox Code Playgroud)

android

4
推荐指数
1
解决办法
5053
查看次数

标签 统计

android ×1