1

I am making a really simple android application that will display a picture after a button is clicked. I have searched for days and tried everything I can think of but cant seem to get it to work. I have been able to host the picture online and link to it but I want the content available offline too. Please help, sorry for the stupid question.

Update:

It is a fixed image and in my drawable resource. Here is the current code I am using to display the image from a URL. What changes should I make to to display that same image from my drawable resource?

JAVA

public class StandingOrders extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void buttonClick (View image)
    { 
        Uri uri = Uri.parse("my url");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent) 
    }
}

XML

<Button android:id="@+id/btn1" 
        android:layout_height="wrap_content" 
        android:layout_width="170px" 
        android:text="The Button" 
        android:layout_gravity="center" 
        android:clickable="true"
        android:onClick="buttonClick">
</Button>
Community
  • 1
  • 1
Timothy
  • 11
  • 1
  • 1
  • 2

8 Answers8

0

Check out this answer: https://stackoverflow.com/a/4896272/458968

In your case, the uri should be

Uri.parse("android.resource://com.company.app/"+R.drawable.my_image);
Community
  • 1
  • 1
you786
  • 3,659
  • 5
  • 48
  • 74
0

You can try to implement the method, which I am posting below. I have done something more or less similar to your requirement.

Method:

   //method to zoom images

                                        public void zoomImage(String imageUrl)
                                        {
                                            AlertDialog.Builder builder;


                                            Context mContext = ExamActivity.this;
                                            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
                                            View layout = inflater.inflate(R.layout.custom_dialog,
                                                                           (ViewGroup) findViewById(R.id.layout_root));



                                            //to close dialog
                                            ImageView close_dialog = (ImageView) layout.findViewById(R.id.imageView_custom_dialog_close);

                                            close_dialog.setOnClickListener(new View.OnClickListener() {

                                                public void onClick(View v) {


                                                alertDialog.dismiss();

                                                }
                                            });

                                            //to show image
                                            WebView wv=(WebView) layout.findViewById(R.id.show_image_webView);
                                            wv.getSettings().setBuiltInZoomControls(true);
                                            wv.setInitialScale(200);
                                            wv.loadUrl(imageUrl);


                                            builder = new AlertDialog.Builder(mContext);
                                            builder.setView(layout);
                                            alertDialog = builder.create();

                                            alertDialog.show();

                                        }

the imageUrl is the url to the webpage containing the image. If you want to show a local image then just create a simple html page containing the image and use the local url.

kittu88
  • 2,451
  • 5
  • 40
  • 80
0

Try this:-

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 ImageView imageToDisplay = (ImageView) findViewById(R.id.imageToDisplay);
 Button btnShowImage = (Button) findViewById(R.id.btnShowImage);
 b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    imageToDisplay.setVisibility(View.VISIBLE); 
    imageToDisplay.setImageResource(R.drawable.imagelogo);
    }
});
  }
}

In layout imageToDisplay's visibility is gone.

Ajay P. Prajapati
  • 1,973
  • 1
  • 17
  • 34
AmDroid
  • 131
  • 5
0

try following example

you can download "android-query-0.22.10.jar" file from following url.

http://www.java2s.com/Code/Jar/a/Downloadandroidquery02210jar.htm

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.addbuttondynamic.AddButtonDynamic" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load Image" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

</LinearLayout>

and Main.java

import com.androidquery.AQuery;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Main extends Activity {

    Button btnLoad;
    ImageView ivImage;
    AQuery aQuery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnLoad=(Button)findViewById(R.id.button1);
        ivImage=(ImageView)findViewById(R.id.imageView1);
        aQuery=new AQuery(this);
        btnLoad.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //load image from /res folder.

                ivImage.setImageResource(R.drawable.ic_launcher);

                //load image from url.

                aQuery.id(ivImage).image("you URL");

                // if ivImage is not working in aQuery.id() then use R.id.imageView1.
            }
        });

    }
}
Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16
0

activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.imagechange.MainActivity" 
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:text="@string/khilan" 
        android:onClick="btnOnClick"
        />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="800px"
        android:layout_height="500px"
        android:layout_marginTop="70dp"/>

</RelativeLayout>

MainActivity.java

    public class MainActivity extends Activity
     {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
    public void btnOnClick (View view)
    {
        String imagename = "khilan";
        int res = getResources().getIdentifier(imagename, "drawable", getPackageName());
        ImageView iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageResource(res);
    }
}
KhiLan PaTeL
  • 123
  • 2
  • 4
0

If it's a fixed image, include it as a drawable resource and use an ImageView to display it. If it's an image to download, you can do that in a separate thread and store it in a file or an SQLite data base, then again use an ImageView to see it.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Okay. It is a fixed image and in my drawable resource. Here is the current code I am using to display the image from a URL. What changes should I make to to display that same image from my drawable resource? – Timothy Jan 26 '11 at 23:35
  • JAVA public class StandingOrders extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void buttonClick (View image) { Uri uri = Uri.parse("my url"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent) } } – Timothy Jan 26 '11 at 23:36
  • Your current coded displays the image in a separate activity. If you want to still do that, declare a layout consisting of a single ImageView and define a new activity class. In the activity's onCreate(), after setting the content view to that layout, retrieve the ImageView and call setImageResource (like pydave suggested above). – Ted Hopp Jan 27 '11 at 17:56
0

Add an ImageView to your xml layout and call setImageResource from onClick.

setImageResource(R.drawable.yourImage);

Or you could set the image in your layout and make the ImageView hidden until you click the button. See setVisibility(View.GONE)

idbrii
  • 10,975
  • 5
  • 66
  • 107
0

Create a layout with image and button, make the image hidden and onClick set the visibility to VISIBLE.

Here is what the XML should look like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:src="@drawable/image1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Image" />
</RelativeLayout>

and the following is the Java code

ImageView image = findViewById(R.id.image);
Button button = findViewById(R.id.got_it_button);
image.setVisibility(View.GONE);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            image.setVisibility(View.VISIBLE);
        }
    });

Good luck :)

Akanshi Srivastava
  • 1,160
  • 13
  • 24