0

I add a new image to Camera2Fragement class to display bitmap image in an alert dialog but I only get empty dialog without any image. What am i doing wrong. Is it related to fragment where we can not upload the image to image view or another problem. Here is my code:

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;
            }
        }
    }
Nani
  • 97
  • 13

2 Answers2

0
   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>
Diksha Pruthi
  • 276
  • 2
  • 5
-1

You are trying to set image to ImageView inside of AlertDialog, which is not shown. Rearange your code like this:

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);
dniHze
  • 2,162
  • 16
  • 22
  • Thanks for your answer. I tried your answer but still it didn't work. @Artyom Dorosh – Nani Jul 20 '17 at 08:47
  • @Nani it seems like you loading data from internal storage. Do you have a permission to access internal storage? And it seems you are using incorrect path to your image. – dniHze Jul 20 '17 at 09:04
  • Thanks, it worked when i set the image using: imgRefInflated.setImageBitmap(result); – Nani Jul 20 '17 at 09:12
  • Both comments are right. Thank you. I have a difficulty in accessing internal storage which always come up with Null error while I can access external storage both writing and reading. Please can you help me by giving links to how to use internal storage in Fragment. – Nani Jul 20 '17 at 09:16
  • @Nani here is tutorial from Android Developers how to access storage: [link](https://developer.android.com/training/basics/data-storage/files.html). And here is how to manage permissions from `Fragment`: [link](https://gist.github.com/MariusVolkhart/618a51bb09c4fc7f86a4). Notice: you have to use support v4 Fragment instead of platform default one. Also don't forget to accept answer if it helps you. :) – dniHze Jul 20 '17 at 09:25
  • Thanks for the links. Can I keep both import android.support.v13.app.FragmentCompat; import android.support.v4.app.Fragment; – Nani Jul 20 '17 at 09:57
  • @Nani yes, this is completely safe to do. – dniHze Jul 20 '17 at 10:05