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