简体   繁体   中英

Display image in popout window after button is clicked — Android/Java

I've been searching the internet for a solution to this problem for several hours now and I can't seem to find an answer that works. I have written an android app that uses the camera emulator in Eclipse to take a photo and save it to the sd card (path is stored using a String called "file"). What I would like to do is display the image, preferably in a sort of pop-out window, with another button that closes it and returns to the app. I expect I'll need to create another class to do this, but I am not sure all that is involved. Will I have to create another XML file to do the layout? How could I access the file and display it? I expect I'll use ImageView if I am to use another class, but other than that I'm not sure what to do. There's not much code I can really provide that would help, other than the method which is called when the button is clicked. This part of the code works just fine, and it's simply a method that looks like this:

public void viewPhoto(file){ }

As you can see, I have nothing inside the method yet, that is why I am posting here. I have been trying to figure this out for several hours, so I would greatly appreciate any ideas you folks might have.

EDIT: MORE INFO

Many of the suggestions I have found require the image to be stored in the "src" folder of the project. However, this photo will be taken and used all within the same program, so I'll have to be able to access it using its file path alone. I can't just state its location in the XML. I created a Dialog object within my existing code, but I'm not sure if this will work. The dialog I plan to use should be able to access the photo from the specified path that is stored in the string mentioned before.

I have been referring to the first answer in this thread: Android Image Dialog/Popup but as you can see it requires the image to be in the src folder and specified in the XML.

SECOND EDIT:

Here is the code I currently have, both for the XML and the Java. But first I want to mention that I have two XML files--one called activity_main.xml which handles all the initial buttons, menu, etc. The second one is only for my photo (called photo_viewer.xml) and it only contains the ImageView.

photo_viewer.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pictureViewer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/viewImage" />

</RelativeLayout>

Java method to generate image:

public void viewPhoto(String file){ 
    ImageView imageView = new ImageView(getApplicationContext());
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    Bitmap image = BitmapFactory.decodeFile(file);
    imageView.setImageBitmap(image);
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.pictureViewer);
    rl.addView(imageView, lp);
}

you need to create a class which would extend Dialog Class.

refer this how you can create your own dialog.

How to create a Custom Dialog box in android?

then for showing image just call DialogObj.show() to show dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM