0

我有图像滑块,其中包含通过图像 url 的图像。所以,当我选择任何特定图像时,单击按钮上的图像应该转到另一个活动。我正在获取图像 url,但它没有在另一个活动上显示图像。所以请帮助我。提前致谢。

我在 Main 中有 String[] imageUrl

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"};

单击按钮时,我将这些 url 发送到另一个活动

主.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);                  
                                            
                    }           
                });

在另一项活动中,我得到了这些 url 但不是图像

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();
                }
            
        }
      
4

3 回答 3

0

在此代码中,您只能获得 url。您没有显示任何图像。

由于您有一个 url,也许您应该尝试在 webview 示例中显示它们:

webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://l.yimg.com/a/i/us/we/52/21.gif");
于 2013-11-12T10:28:21.170 回答
0

这是我的代码。这是工作。请将您在 Androidmanifest 中的 minsdkversion 更改为 9。StrictPolicy 要求最低 sdk 版本为 9

主要活动.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>
于 2013-11-12T11:56:25.697 回答
0

主.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);
    }
}

第二活动

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);
        }


    }
}
于 2013-11-13T09:09:55.190 回答