0
我想在图像视图中添加图片,并在一行中显示文本。此代码正在打开画廊并让我选择一张照片。但是当图片被选中时,图片并未在图像中显示。我把代码从这个链接 [Image browse button in android activity通过用户选择在图像视图中添加图片
这里是我的代码
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
和
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null
!= data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
这里是XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:padding="1dp"
android:background="@color/Black"
android:layout_marginTop="10dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_weight="0.5"
/>
<TextView
android:layout_marginTop="10dp"
android:text="TextView"
android:layout_width="match_parent"
android:singleLine="true"
android:layout_gravity="right"
android:background="@color/White"
android:layout_height="wrap_content"
android:id="@+id/child"
android:textSize="24sp"/>
</LinearLayout>
我没有忘记添加它.. – khanzada
您可以使用Log.d(“TAG”,“picturePath:”+ picturePath); 如果日志不显示,您可以检查构建版本代码,Android版本M或更高要求的权限读取外部存储。 如果日志显示,您可以检查绝对路径图像并搜索“从外部存储装载位图”。 我希望我的回答能帮助你! – Dungnbhut