0

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);
}
Community
  • 1
  • 1
DerStrom8
  • 1,311
  • 2
  • 23
  • 45

1 Answers1

1

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.

Community
  • 1
  • 1
Kirtan
  • 1,782
  • 1
  • 13
  • 35
  • Hmm, that might be just what I need. I guess the only thing now is how to create a separate layout XML file for the dialog....? – DerStrom8 Nov 23 '13 at 18:31
  • the link i given has each and every detail about how to create dialog layout in separate xml and how to inherit Dialog class. – Kirtan Nov 23 '13 at 18:32
  • many of the suggestions require the image to be in the "src" folder. 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. For example, I created a second XML but did not create a second class for the dialog. Instead I created a Dialog object within my existing code. First of all I'm not sure if this will work, but I figured I'd ask. Second, I need to make sure the dialog displays the image from the specified path. Thoughts? – DerStrom8 Nov 23 '13 at 19:24
  • I have edited the original post to make this clearer. Sorry about that. – DerStrom8 Nov 23 '13 at 19:29
  • The difference for your needs then, is that you need to create a Bitmap or Drawable object from the file, and put that in your widget. BTW, The Big Nerd Ranch android book has a project that uses the camera to do exactly this. – Rick Falck Nov 23 '13 at 20:48
  • I have tried using Bitmap and Drawable objects to make it work and still have not had any luck. I am considering starting a new question as I have a little more information (plus some code), but I think I'll just add it in an edit to my original post. The issue I am having now is that it crashes every time I try to view the image. I know it's an obvious error, I just can't seem to put my finger on it. – DerStrom8 Nov 23 '13 at 21:28