这应该比以前容易多了..。但我只是试图从URI获得图像的完整路径。
String[] projection = { MediaStore.MediaColumns.DISPLAY_NAME};将正确显示文件名,但:
String[] projection = { MediaStore.MediaColumns.DATA}; 或者..。
String[] projection = { MediaStore.Images.Media.DATA};...will只给我空。
也许我太累了,思考不清楚(这是一个大得多的应用程序中的一个非常小的部分),但我不太明白,如果图像清晰,位图工作正常,显示名称工作正常,那怎么可能呢?我肯定我错过了一些简单的东西..。我只是现在看不见。
完整的代码如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = null;
if (data != null) {
selectedImageUri = data.getData();
String[] projection = { MediaStore.MediaColumns.DISPLAY_NAME};
ContentResolver cr = this.getApplicationContext().getContentResolver();
Cursor metaCursor = cr.query(selectedImageUri,
projection, null, null, null);
metaCursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
if (metaCursor != null) {
try {
if (metaCursor.moveToFirst()) {
imagepath = metaCursor.getString(0);
Log.i(TAG, "UriB: " + metaCursor.getString(0));
}
} finally {
metaCursor.close();
}
}
Log.i(TAG, "Uri: " + selectedImageUri.toString());
}
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImageUri));
imageview.setImageBitmap(bitmap);
messageText.setText("Uploading file path: " + imagepath);
} catch (IOException ie) {
messageText.setText("Error");
}
}
}androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jon.bon" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity" >
</activity>
</application>我知道这上面有很多页面,因为我现在已经打开了大约7页,但是我不知道为什么我会被卡住。
编辑
看起来我可能不得不使用EXTERNAL_CONTENT_URI了。但当我这么做的时候
Cursor metaCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);...I得到了一些完全随机的(嗯.实际上,有一个非常具体的)路径,不管我尝试使用什么图像。好像这根本就没有关系。
发布于 2017-11-12 04:44:06
如果权限是启用的,下面的代码将适用于'Intent.ACTION_PICK‘。
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, Constants.PICK_GALLERY);如果没有,则添加设备规范。OnActivity的结果如下。
if(responseCode == activity.RESULT_OK && requestCode==Constants.PICK_GALLERY){
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
try {
Cursor c = activity.getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
if(picturePath==null) {
picturePath=selectedImage.getPath();
}
//Use picturePath for setting bitmap or to crop
}catch(Exception e)
{
e.printStackTrace();
}
}https://stackoverflow.com/questions/47244481
复制相似问题