我试图从图库中获取所选文件的路径,但它返回null并且我不知道原因。我看到的每个代码都使用相同的方法,但它对我不起作用。这是我的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// LOAD_FILE_REQUEST is a global variable:
// private static final int LOAD_FILE_REQUEST = 1;
if (requestCode == LOAD_FILE_REQUEST && resultCode == RESULT_OK && data != null) {
if(data.getData() == null) {
System.out.println("NULL");
} else {
System.out.println("NOT NULL"); // <--- Printed
}
currImageURI = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(currImageURI, filePathColumn, null, null, null);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String yourRealPath = cursor.getString(columnIndex);
System.out.println("REAL PATH "+yourRealPath);
} else {
System.out.println("NO ROWS!!!"); // <-- Not printed
}
cursor.close();
}
}
答案 0 :(得分:2)
您是否添加了该行
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
? =)
(有这个问题^^)
答案 1 :(得分:2)
嗯,这就是我在动态壁纸中的表现(Noiraude,看看:P)
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 100:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
@SuppressWarnings("unused")
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sharedPreferences = getSharedPreferences("NLP_settings", 0);
Editor editor = sharedPreferences.edit();
editor.putString("key_bit", getPath(selectedImage));
editor.commit();
restartThis();
}
}
}
public String getPath(Uri uri) {
// just some safety built in
if (uri == null) {
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
switch (requestCode) {
case 100:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
@SuppressWarnings("unused")
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sharedPreferences = getSharedPreferences("NLP_settings", 0);
Editor editor = sharedPreferences.edit();
editor.putString("key_bit", getPath(selectedImage));
editor.commit();
restartThis();
}
}
}
public String getPath(Uri uri) {
// just some safety built in
if (uri == null) {
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
答案 2 :(得分:1)
你能解释一下你的问题吗?你在哪里得到一个空?
您在哪个Android版本中运行此代码?从Android 4.4开始,当您发送拾取图像的意图时打开的文件选择器返回相对uri,因为它不仅显示存储在设备中的文件,还显示存储在云中的文件。因此,您可能会发现您正在获取相对URI,当您在设备上查询它的位置时,您将获得null,因为ContentResolver没有路径那个文件。
如果是这样的话(事实上,即使你不是,因为你应该开发兼容Android和新版本的应用程序)我建议你使用Content Resvolver打开一个InputStream来获取文件(openInputStream(Uri),因为它允许你从任何位置(本地和云)获取文件。
我希望它有所帮助:)