android中怎么將圖片路徑與Uri相互轉(zhuǎn)換?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
余姚網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司成立與2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)公司。
一個android文件的Uri地址一般如下:
content://media/external/images/media/62026
這是一張圖片的Uri,那么我們?nèi)绾胃鶕?jù)這個Uri獲得其在文件系統(tǒng)中的路徑呢?
其實很簡單,直接上代碼:
public static String getRealFilePath( final Context context, final Uri uri ) { if ( null == uri ) return null; final String scheme = uri.getScheme(); String data = null; if ( scheme == null ) data = uri.getPath(); else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) { data = uri.getPath(); } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) { Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, null, null, null ); if ( null != cursor ) { if ( cursor.moveToFirst() ) { int index = cursor.getColumnIndex( ImageColumns.DATA ); if ( index > -1 ) { data = cursor.getString( index ); } } cursor.close(); } } return data }
如我們有一個圖片的路徑地址又該如何獲得其Uri呢?
String type = Utils.ensureNotNull(intent.getType()); Log.d(TAG, "uri is " + uri); if (uri.getScheme().equals("file") && (type.contains("image/"))) { String path = uri.getEncodedPath(); Log.d(TAG, "path2 is " + path); if (path != null) { path = Uri.decode(path); Log.d(TAG, "path3 is " + path); ContentResolver cr = this.getContentResolver(); StringBuffer buff = new StringBuffer(); buff.append("(") .append(Images.ImageColumns.DATA) .append("=") .append("'" + path + "'") .append(")"); Cursor cur = cr.query( Images.Media.EXTERNAL_CONTENT_URI, new String[] { Images.ImageColumns._ID }, buff.toString(), null, null); int index = 0; for (cur.moveToFirst(); !cur.isAfterLast(); cur .moveToNext()) { index = cur.getColumnIndex(Images.ImageColumns._ID); // set _id value index = cur.getInt(index); } if (index == 0) { //do nothing } else { Uri uri_temp = Uri .parse("content://media/external/images/media/" + index); Log.d(TAG, "uri_temp is " + uri_temp); if (uri_temp != null) { uri = uri_temp; } } } }
看完上述內(nèi)容,你們掌握android中怎么將圖片路徑與Uri相互轉(zhuǎn)換的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站名稱:android中怎么將圖片路徑與Uri相互轉(zhuǎn)換
當前地址:http://vcdvsql.cn/article8/gjoiop.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、品牌網(wǎng)站制作、網(wǎng)站內(nèi)鏈、響應式網(wǎng)站、微信公眾號、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)