在片段的警告对话框中显示位图图像
Display bitmap image in an alert dialog in fragment
我将新图像添加到 Camera2Fragement class 以在警告对话框中显示位图图像,但我只得到没有任何图像的空对话框。我究竟做错了什么。是否与我们无法将图像上传到图像视图或其他问题的片段有关。这是我的代码:
public void onClick(View view) {
switch (view.getId()) {
case R.id.picture: {
takePicture();
break;
}
case R.id.info: {
Activity activity = getActivity();
if (null != activity) {
new AlertDialog.Builder(activity)
.setMessage(R.string.intro_message)
.setPositiveButton(android.R.string.ok, null)
.show();
}
break;
}
case R.id.result: { // added code
Activity activity = getActivity();
if (null != activity) {
view = View.inflate(activity, R.layout.dialog_layout, null);
ImageView imgRefInflated = (ImageView) view.findViewById(R.id.dialog_imageview);
Picasso.with(activity).load("/mnt/sdcard/DCIM/mResultImg.jpg").into(imgRefInflated);
//imgRefInflated.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/DCIM/mResultImg.jpg"));
Log.d(TAG, "I can reach here");
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.setView(view);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();
}
break;
}
}
}
您正在尝试将图像设置为 AlertDialog 内的 ImageView,但未显示。像这样重新排列您的代码:
view = View.inflate(activity, R.layout.dialog_layout, activity);
AlertDialog dialog = new AlertDialog.Builder(this)
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setView(view)
.create();
dialog.show();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
ImageView imgRefInflated = (ImageView) view.findViewById(R.id.dialog_imageview);
Picasso.with(activity).load("/mnt/sdcard/DCIM/mResultImg.jpg").into(imgRefInflated);
val myBitmap = BitmapFactory.decodeFile(f.absolutePath)
val builder = AlertDialog.Builder(this)
val dialog = builder.create()
val inflater: LayoutInflater = layoutInflater
val dialogLayout: View = inflater.inflate(R.layout.file_viewer_alert, null)
val imageview = dialogLayout.findViewById(R.id.image_view) as AppCompatImageView
val ivCross = dialogLayout.findViewById(R.id.ivCross) as AppCompatImageView
ivCross.setOnClickListener {
dialog.dismiss()
}
imageview.setImageBitmap(myBitmap)
dialog.setView(dialogLayout)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setCanceledOnTouchOutside(false)
dialog.show()
file_viewer_alert.xml
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:gravity="center"
android:layout_margin="@dimen/stroke_large"
android:layout_height="wrap_content"
android:background="@drawable/solid_white_curve">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_view"
android:layout_width="@dimen/logo_width"
android:layout_height="@dimen/logo_width"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginBottom="@dimen/medium_margin"
android:scaleType="fitCenter"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivCross"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:src="@drawable/cancel_orange" />
</FrameLayout>
我将新图像添加到 Camera2Fragement class 以在警告对话框中显示位图图像,但我只得到没有任何图像的空对话框。我究竟做错了什么。是否与我们无法将图像上传到图像视图或其他问题的片段有关。这是我的代码:
public void onClick(View view) {
switch (view.getId()) {
case R.id.picture: {
takePicture();
break;
}
case R.id.info: {
Activity activity = getActivity();
if (null != activity) {
new AlertDialog.Builder(activity)
.setMessage(R.string.intro_message)
.setPositiveButton(android.R.string.ok, null)
.show();
}
break;
}
case R.id.result: { // added code
Activity activity = getActivity();
if (null != activity) {
view = View.inflate(activity, R.layout.dialog_layout, null);
ImageView imgRefInflated = (ImageView) view.findViewById(R.id.dialog_imageview);
Picasso.with(activity).load("/mnt/sdcard/DCIM/mResultImg.jpg").into(imgRefInflated);
//imgRefInflated.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/DCIM/mResultImg.jpg"));
Log.d(TAG, "I can reach here");
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.setView(view);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();
}
break;
}
}
}
您正在尝试将图像设置为 AlertDialog 内的 ImageView,但未显示。像这样重新排列您的代码:
view = View.inflate(activity, R.layout.dialog_layout, activity);
AlertDialog dialog = new AlertDialog.Builder(this)
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setView(view)
.create();
dialog.show();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
ImageView imgRefInflated = (ImageView) view.findViewById(R.id.dialog_imageview);
Picasso.with(activity).load("/mnt/sdcard/DCIM/mResultImg.jpg").into(imgRefInflated);
val myBitmap = BitmapFactory.decodeFile(f.absolutePath)
val builder = AlertDialog.Builder(this)
val dialog = builder.create()
val inflater: LayoutInflater = layoutInflater
val dialogLayout: View = inflater.inflate(R.layout.file_viewer_alert, null)
val imageview = dialogLayout.findViewById(R.id.image_view) as AppCompatImageView
val ivCross = dialogLayout.findViewById(R.id.ivCross) as AppCompatImageView
ivCross.setOnClickListener {
dialog.dismiss()
}
imageview.setImageBitmap(myBitmap)
dialog.setView(dialogLayout)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setCanceledOnTouchOutside(false)
dialog.show()
file_viewer_alert.xml
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:gravity="center"
android:layout_margin="@dimen/stroke_large"
android:layout_height="wrap_content"
android:background="@drawable/solid_white_curve">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_view"
android:layout_width="@dimen/logo_width"
android:layout_height="@dimen/logo_width"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginBottom="@dimen/medium_margin"
android:scaleType="fitCenter"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/ivCross"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:src="@drawable/cancel_orange" />
</FrameLayout>