0

I am having image slider which are containing images through image url.So,when I select any particular image that image on button click should go to another activity.I am getting image url but its not displaying image on another activity.So please help me.Thanks in advance.

I am having String[] imageUrl in Main

String[] imageUrl={"http://l.yimg.com/a/i/us/we/52/21.gif","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};

On button click I am sending these url to another activity

Main.java

 String[] imageUrl={"http://l.yimg.com/a/i/us/we/52/21.gif","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};
    
    
        Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
        btnNextScreen.setOnClickListener(new View.OnClickListener() {           
                                        
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(Main.this, OpenImage.class);
                        intent.putExtra("some_key", imageUrl);
                        startActivity(intent);                  
                                            
                    }           
                });

In another activity I am getting these url but not a image

OpenImage.java

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen2);
            String[] data = getIntent().getExtras().getStringArray("some_key");
            for(String x:data) {
                Log.i(TAG, "x="+x);
                Toast.makeText(OpenImage.this, x, Toast.LENGTH_SHORT).show();
                }
            
        }
      
Community
  • 1
  • 1

3 Answers3

0

In this code you only get urls. You are not displaying any image.

As you have an url, maybe you should try to display them in a webview Example:

webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://l.yimg.com/a/i/us/we/52/21.gif");
Gyonder
  • 3,674
  • 7
  • 32
  • 49
  • this is working but my question is different, my url is decided dynamically..means in my code there are 3 url when i execute app and if i select last image then it should display last image in another activity and similarly for 1st ,2nd – user2843124 Nov 12 '13 at 11:03
  • but where are you going to display it and how? – Gyonder Nov 12 '13 at 11:04
  • i am going to display it on another activity please have a look on my code u will get it.If any doubt please feel free to ask. – user2843124 Nov 12 '13 at 11:13
  • What I don't get , is where do you want to display this image in this second activity? In an imageView? – Gyonder Nov 12 '13 at 11:15
  • Then either you use webview (instead of imageview) as I wrote in answer or you should download the image. http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – Gyonder Nov 12 '13 at 11:21
  • thnx for link i have already tried this but i want to ask new DownloadImageTask((ImageView) findViewById(R.id.imageview)) .execute("XXXXX"); What to write inplace of "XXXX" for selected image from slider in that code there was static link which i dont want i want selected image link – user2843124 Nov 12 '13 at 11:34
0

Here is my code. It is working. Please change your minsdkversion in Androidmanifest to 9. StrictPolicy require minimum sdk version to be 9

mainactivity.java

    package com.example.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {
    ImageView image1,image2;
    Bitmap bitmap;
    final static String imageLocation="http://www.codeincloud.tk/play.png"; //Use any image location. 
    String[] imageUrl={"http://l.yimg.com/a/i/us/we/52/21.gif","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image1 = (ImageView)findViewById(R.id.imageView1);
        image2 = (ImageView)findViewById(R.id.imageView2);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        URL imageURL = null;

        try {
            for(int i=0;i<imageUrl.length;i++){
                imageURL = new URL(imageUrl[i]);

                HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream inputStream = connection.getInputStream();

                bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap

                if(i == 0)
                    image1.setImageBitmap(bitmap);

                if(i == 1)
                    image2.setImageBitmap(bitmap);

            }
        }
        catch (IOException e) {

            e.printStackTrace();
        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

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=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="69dp"
        android:layout_marginTop="106dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="183dp"
        android:layout_marginTop="45dp"
        android:layout_toRightOf="@+id/imageView1"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66
  • My code takes an url and shows the pic in an imageview.You said that you are getting url but image is not showing so i provided the code for showing the image – Rahul Gupta Nov 12 '13 at 12:38
  • yes but i am using String[] imageUrl which are containing several urls and you are using single url – user2843124 Nov 12 '13 at 12:50
  • You have to run my code for the number of times the length of your string url array. Get the length of your array in the second activity.Create that many imageviews accordingly Run a for loop. Inside the for loop paste my code. Simple – Rahul Gupta Nov 12 '13 at 12:53
  • can please upload your code..it will be easy for understanding – user2843124 Nov 12 '13 at 13:03
  • i want to send selected image on another page on button click..so, where u have written on button click code?? – user2843124 Nov 12 '13 at 13:47
  • how do you select an image from slider ? Please edit your post and add that code too. – Rahul Gupta Nov 12 '13 at 14:46
0

Main.java

    String[] imageUrl={"https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png","http://www.codeincloud.tk/play.png",
                             "https://devimages.apple.com.edgekey.net/contact/images/technical-icon.png"};  


        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }

            imgView = (ImageView)findViewById(R.id.ImageViewTops);  
            imgView.setImageResource(Imgid[0]);      

            Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
            btnNextScreen.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                    String image_selected = imageUrl[selected_image];
                    sendMessage(image_selected);
                }

            });

            topsgallery = (Gallery) findViewById(R.id.gallery1);
            topsgallery.setAdapter(new AddImgAdp (this));

            topsgallery.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {

                    selected_image = position;
                    Log.d("Selected","Image url="+selected_image);
                    imgView = (ImageView)findViewById(R.id.ImageViewTops);
                }
             });    

     }
public void sendMessage(String image_selected) {
        Intent intent = new Intent(this, SecondActivity.class);
        String image_url = imageUrl[selected_image];
        intent.putExtra(EXTRA_MESSAGE, image_url);
        startActivity(intent);
    }
}

SecondActivity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);

        setupActionBar();
        Intent intent = getIntent();
        Context context = getApplicationContext();
        String image_url = intent.getStringExtra(Main.EXTRA_MESSAGE);
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, image_url, duration);
        toast.show();
        new DownloadImageTask((ImageView) findViewById(R.id.imageview))
        .execute(image_url);    

    }
    private void setupActionBar() {
        // TODO Auto-generated method stub

    }
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        @SuppressWarnings("unused")
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            //Log.i("url","urldisplay="+urldisplay);
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            ImageView bmImage = (ImageView)findViewById(R.id.imageview);
            bmImage.setImageBitmap(result);
        }


    }
}