我正在通过图片网址包含图片的图片滑块。所以,当我选择任何特定的图片时,按钮点击的图片应该转到另一个活动。我正在获取图片网址,但它没有显示图像请帮助我。谢谢。
我在Main
中有String [] imageUrlString[] 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"};
点击按钮,我将这些网址发送到另一个活动
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);
}
});
在另一项活动中,我收到这些网址但不是图片
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();
}
}
答案 0 :(得分:0)
在此代码中,您只能获得网址。您没有显示任何图像。
如果您有网址,也许您应该尝试在网页浏览中显示它们 例如:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://l.yimg.com/a/i/us/we/52/21.gif");
答案 1 :(得分:0)
这是我的代码。这是工作。请将Androidmanifest中的minsdkversion更改为9。 StrictPolicy要求最低sdk版本为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>
答案 2 :(得分: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);
}
}
}