在棒棒糖的android始终文件路径返回NULL [英] file path return null always in lollipop android
本文介绍了在棒棒糖的android始终文件路径返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是当我从内部存储(库)获得的图像我的code。 在棒棒糖文件路径返回始终为空。
如果(要求code == PICK_IMAGE){
如果(结果code == RESULT_OK){
//形象成功地挑
//发射载活动
乌里selectedImage = data.getData();
的String [] filePathColumn = {MediaStore.Images.Media.DATA};
光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst();
参数:columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
FILE_PATH = cursor.getString(参数:columnIndex);
Log.d(的getClass()的getName(),FILE_PATH+ FILE_PATH);
=了fileURI Uri.parse(文件://+ FILE_PATH);
cursor.close();
launchUploadActivity(真,PICK_IMAGE);
}否则,如果(结果code == RESULT_CANCELED){
//用户取消记录
Toast.makeText(getApplicationContext(),用户取消图像选择,Toast.LENGTH_SHORT).show();
} 其他 {
//无法录制视频
Toast.makeText(getApplicationContext()!对不起未能挑形象,Toast.LENGTH_SHORT).show();
}
解决方案
感谢名单的是,我找到了解决办法。
乌里selectedImage = data.getData();
字符串wholeID = DocumentsContract.getDocumentId(selectedImage);
//分割在结肠,使用数组中的第二项
串的id = wholeID.split(:)[1];
字符串[]列= {MediaStore.Images.Media.DATA};
//其中id等于
字符串SEL = MediaStore.Images.Media._ID +=;
光标光标= getContentResolver()。
查询(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
列,SEL,新的String [] {ID},NULL);
字符串文件路径=;
INT参数:columnIndex = cursor.getColumnIndex(列[0]);
如果(cursor.moveToFirst()){
文件路径= cursor.getString(参数:columnIndex);
}
cursor.close();
setImageFromIntent(文件路径);
this is my code when i'm getting image from internal storage (gallery). In lollipop file path return always null.
if (requestCode == PICK_IMAGE) {
if(resultCode == RESULT_OK){
//image successfully picked
// launching upload activity
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnindex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
file_path = cursor.getString(columnindex);
Log.d(getClass().getName(), "file_path"+file_path);
fileUri = Uri.parse("file://" + file_path);
cursor.close();
launchUploadActivity(true, PICK_IMAGE);
}else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
Toast.makeText(getApplicationContext(),"User cancelled image selection", Toast.LENGTH_SHORT).show();
} else {
// failed to record video
Toast.makeText(getApplicationContext(),"Sorry! failed to pick image", Toast.LENGTH_SHORT).show();
}
解决方案
Thanx all,I found the solution.
Uri selectedImage = data.getData();
String wholeID = DocumentsContract.getDocumentId(selectedImage);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
String filePath = "";
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
setImageFromIntent(filePath);
这篇关于在棒棒糖的android始终文件路径返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文