/
根据Uri获取绝对路径,解决Android44以上版本Uri转换
@param activity
@param imageUri
@author yaoxing
@date 2014-10-12
/
@TargetApi(19)
public static String getImageAbsolutePath(Activity context, Uri imageUri) {
if (context == null || imageUri == null)
return null;
if (androidosBuildVERSIONSDK_INT >= androidosBuildVERSION_CODESKITKAT && DocumentsContractisDocumentUri(context, imageUri)) {
if (isExternalStorageDocument(imageUri)) {
String docId = DocumentsContractgetDocumentId(imageUri);
String[] split = docIdsplit(":");
String type = split[0];
if ("primary"equalsIgnoreCase(type)) {
return EnvironmentgetExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(imageUri)) {
String id = DocumentsContractgetDocumentId(imageUri);
Uri contentUri = ContentUriswithAppendedId(Uriparse("content://downloads/public_downloads"), LongvalueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(imageUri)) {
String docId = DocumentsContractgetDocumentId(imageUri);
String[] split = docIdsplit(":");
String type = split[0];
Uri contentUri = null;
if ("image"equals(type)) {
contentUri = MediaStoreImagesMediaEXTERNAL_CONTENT_URI;
} else if ("video"equals(type)) {
contentUri = MediaStoreVideoMediaEXTERNAL_CONTENT_URI;
} else if ("audio"equals(type)) {
contentUri = MediaStoreAudioMediaEXTERNAL_CONTENT_URI;
}
String selection = MediaStoreImagesMedia_ID + "=";
String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} // MediaStore (and general)
else if ("content"equalsIgnoreCase(imageUrigetScheme())) {
// Return the remote address
if (isGooglePhotosUri(imageUri))
return imageUrigetLastPathSegment();
return getDataColumn(context, imageUri, null, null);
}
// File
else if ("file"equalsIgnoreCase(imageUrigetScheme())) {
return imageUrigetPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
String column = MediaStoreImagesMediaDATA;
String[] projection = { column };
try {
cursor = contextgetContentResolver()query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursormoveToFirst()) {
int index = cursorgetColumnIndexOrThrow(column);
return cursorgetString(index);
}
} finally {
if (cursor != null)
cursorclose();
}
return null;
}
/
@param uri The Uri to check
@return Whether the Uri authority is ExternalStorageProvider
/
public static boolean isExternalStorageDocument(Uri uri) {
return "comandroidexternalstoragedocuments"equals(urigetAuthority());
}
/
@param uri The Uri to check
@return Whether the Uri authority is DownloadsProvider
/
public static boolean isDownloadsDocument(Uri uri) {
return "comandroidprovidersdownloadsdocuments"equals(urigetAuthority());
}
/
@param uri The Uri to check
@return Whether the Uri authority is MediaProvider
/
public static boolean isMediaDocument(Uri uri) {
return "comandroidprovidersmediadocuments"equals(urigetAuthority());
}
/
@param uri The Uri to check
@return Whether the Uri authority is Google Photos
/
public static boolean isGooglePhotosUri(Uri uri) {
return "comgoogleandroidappsphotoscontent"equals(urigetAuthority());
}
使用照相机的方法:
//在监听事件中加入如下代码
Intent intent = new Intent(MediaStoreACTION_IMAGE_CAPTURE);
intentputExtra(MediaStoreEXTRA_OUTPUT, UrifromFile(new File(EnvironmentgetExternalStorageDirectory()+"/","tempjpg")));
startActivityForResult(intent, reqCode_CAMERA);// reqCode_CAMERA是一个一自己设定的值,用于在onActivityResult方法中判断是哪一个activity返回的
再重写onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
superonActivityResult(requestCode, resultCode, data);
if(requestCode == reqCode_CAMERA && resultCode== RESULT_OK){
////这里是直接隐式启动系统预览程序
// Intent intent = new Intent();
// intentsetAction(IntentACTION_VIEW);
//// uri指向用户选择的那个
// Uri uri = datagetData();
// intentsetData(uril);
// startActivity(intent);
// Uri uri = datagetData();
// Cursor cursor = getContentResolver()query(uri, null, null, null, null);
// if(cursor!=null&&cursormoveToFirst()){
// String path = cursorgetString(1);
// Bitmap bm = BitmapFactorydecodeFile(path);
// ivsetImageBitmap(bm);
// }
// ivsetImageURI(uri);
//也可以用这里代码打开裁剪
Intent intent = new Intent();
intentsetAction("comandroidcameraactionCROP");
intentsetType("image/");
intentputExtra("data", datagetExtras()getParcelable("data"));
intentputExtra("crop", "true");
intentputExtra("aspectX", 1);//默认剪裁起始位置x值
intentputExtra("aspectY", 1);//默认剪裁起始位置y值
intentputExtra("outputX", 128);//默认剪裁终止位置x值
intentputExtra("outputY", 128);//默认剪裁终止位置x值
intentputExtra("return-data", true);
startActivityForResult(intent, reqCode_Zoom);
}//这里是处理剪裁返回后的处理,直接调用ImageView显示了,你可以根据你的需要修改
else if(requestCode==reqCode_Zoom){
Bitmap bitmap = datagetExtras()getParcelable("data");
ivsetImageBitmap(bitmap);
}
上传本地方法:
按钮名setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 隐式启动系统相册程序
Intent intent = new Intent();
intentsetAction(IntentACTION_PICK);
intentsetDataAndType(MediaEXTERNAL_CONTENT_URI, "image/");
startActivityForResult(intent, reqCode_PICK);// reqCode和上面一样自己定义,但是不要重复了
}
});
再在onActivityResult方法添加一个"elseif(requestCode == reqCode_PICK && resultCode== RESULT_OK)"条件,方法体直接参考上面,就是在剪裁那里把“intentputExtra("data", datagetExtras()getParcelable("data"));”换为“intentsetDataAndType(datagetData(), "image/");”就可以了。
android下使用cursor获取专辑,主要是在数据库里面先保存专辑的url地址,当使用cursor游标遍历数据库数据的时候,使用资源 *** 作类进行加载相应的url,如下代码:
Cursor cursor = contextgetContentResolver()query(MediaEXTERNAL_CONTENT_URI, columns, null, null, null);
Loge("cursor" , (cursor==null) + "");
while(cursormoveToNext()) {
// 查找封面
long albumId = cursorgetLong(5);
// 读取专辑
String album_uri = "content://media/external/audio/albumart"; // 专辑Uri对应的字符串
Uri albumUri = ContentUriswithAppendedId(Uriparse(album_uri), albumId);
// 取 ==> 得到一个输入流
Bitmap coverPhoto = null ;
try {
InputStream is = contextgetContentResolver()openInputStream(albumUri);
if(null != is) {
coverPhoto = BitmapFactorydecodeStream(is);
}
} catch (Exception e) {
eprintStackTrace();
}
dataadd(new Audio(cursorgetLong(0), cursorgetString(1) , cursorgetString(2) , cursorgetLong(3) , cursorgetString(4) , albumId , coverPhoto));
}
cursorclose();
以上就是关于android4.4版本uri转换怎么调用全部的内容,包括:android4.4版本uri转换怎么调用、请android高手帮忙!、android 下使用Cursor如何获得专辑图片等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)