zoukankan      html  css  js  c++  java
  • android红米等关于读取本地文件夹图片获取路径的问题的解决

    在Android开发中,有从本地文件夹中读取图片的功能,使用一下代码打开图片选择列表:

    Intent intent = new Intent();  
    intent.setAction(Intent.ACTION_PICK);  
    intent.setType("image/*");  
    startActivityForResult(intent, RESULT_LOAD_IMAGE);  
    

    当我们选择一个图片以后,在onActivityResult中,我们用一下代码获取选择的图片路径:

    Uri uri = data.getData();  
    String path = uri.getPath(); 
    

    在这段代码中,我测试发现,在我的Nexus 4、魅族、模拟器上测试没有什么问题,但是在红米,华为等手机上测试,发现得到的path这个路径值,不是我们图片在手机上的路径,暂时还不明白是个什么值,如果根据这个path去获取图片会得到一个null值,不知为什么???

    解决方案:在onActivityResult中,按照如下的方式,就可以在所有的手机上使用,不会出问题,亲测:

    if (data != null) {    
        Uri uri = data.getData();    
        if (!TextUtils.isEmpty(uri.getAuthority())) {    
            Cursor cursor = getContentResolver().query(uri,  
                    new String[] { MediaStore.Images.Media.DATA },null, null, null);    
            if (null == cursor) {    
                Toast.makeText(this, "图片没找到", Toast.LENGTH_SHORT).show();    
                return;    
            }    
            cursor.moveToFirst();    
            path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));  
            cursor.close();    
        } else {    
            path = uri.getPath();    
        }    
    }else{    
        Toast.makeText(this, "图片没找到", Toast.LENGTH_SHORT).show();    
        return;    
    }   
    

      

      

      

  • 相关阅读:
    使用Ruby On Rails建立一个山寨小说站(一)
    sql server 数据库备份概述
    JVM崩溃的原因及解决!
    C++中的字节对齐
    jqueyr用jsonp跨越,有服务器端代码(.net(C#语言))
    lammps_data文件
    jquery日历插件_时间范围_双日历_多日历
    js 判断所选时间(或者当前时间)是否在某一时间段
    webstorm安装时遇到The JVM could not be started的解决方法
    Webstorm快捷键
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/6735937.html
Copyright © 2011-2022 走看看