我写了一些代码,通过使用主题标签在Twitter上搜索图像,并在我的应用程序中显示图像。
这是我的结构。我有一个SearchView,如果用户将一些文本放入SearchView,Twitter4j Query会在Twitter上搜索它。然后我从结果中解析图像网址。
现在我的问题。如何将网址中的图像放入imageView以显示网址后面的图片?
这是我的代码:
/* Setup Configurationbuilder for Twitter4j API, build a searchquery for search Twitter,
search Twitter with the search string from the SearchView
*/
public void searchTweets(String searchString) {
//auth. the application by creating new ConfigurationBuilder
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setJSONStoreEnabled(true);
cb.setOAuthConsumerKey("someSecretStuff");
cb.setOAuthConsumerSecret("someSecretStuff");
cb.setOAuthAccessToken("someSecretStuff");
cb.setOAuthAccessTokenSecret("someSecretStuff");
//start the search by build a new query
TwitterFactory tf = new TwitterFactory(cb.build());
final twitter4j.Twitter twitter = tf.getInstance();
final Query query = new Query(searchString);
//start a new Thread to evaluate the searchQuery
new Thread(new Runnable() {
@Override
public void run() {
try {
//get the results
QueryResult result = twitter.search(query);
//for each result print the user and the the messagetext
for (Status status : result.getTweets()) {
twitter4j.MediaEntity [] media = status.getMediaEntities();
for (twitter4j.MediaEntity m : media) {
System.out.println(m.getMediaURL());
}
}
} catch (TwitterException e) {
e.printStackTrace();
}
}
}).start();
}
更新
嘿伙计,
非常感谢您的所有答案和提示。现在我知道了,如何从网址获取图片,但我仍然有问题在我的imageView中显示它。我想实现一堆图片来滑动他们喜欢和不喜欢。所以我创建一个循环,为每个项目创建一个relativeLayout和一个imageView。现在我试图将url中的图像放入imageView但它不起作用。该应用程序启动,我只看到一个白色的屏幕。
这是我的代码:
//for each picture into the list...
for (int i = 0; i < imageUrl.size(); i++) {
//set RelativeLayout
final RelativeLayout relView = new RelativeLayout(this);
//set Params for the RelativeLayout
relView.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth - 80), windowHeight - 80));
relView.setX(40);
relView.setY(40);
relView.setTag(i);
//set BackgroundColor RelativeLayout
relView.setBackgroundColor(Color.WHITE);
String url = imageUrl.get(i);
//set ImageView
ImageView img = new ImageView(this);
Picasso
.with(this)
.load(url)
.resize(600, 200)
.centerCrop()
.into(img);
//set params to RelativLayout
img.setLayoutParams(new RelativeLayout.LayoutParams((windowWidth), windowHeight));
//show image
relView.addView(img);
//create and set like stamp
final Button imageLike = new Button(this);
imageLike.setLayoutParams(new ActionBar.LayoutParams(100, 50));
imageLike.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_stamp_like));
imageLike.setX(20);
imageLike.setY(80);
imageLike.setAlpha(alphaValue);
relView.addView(imageLike);
//create and set dislike stamp
final Button imagePass = new Button(this);
imagePass.setLayoutParams(new ActionBar.LayoutParams(100, 50));
imagePass.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_stamp_pass));
imagePass.setX((windowWidth - 200));
imagePass.setY(100);
imagePass.setRotation(45);
imagePass.setAlpha(alphaValue);
relView.addView(imagePass);
//create touchlistener for slide
final int finalI = i;
relView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//get screen coordinates
x_cord = (int) event.getRawX();
y_cord = (int) event.getRawY();
//set coordinates to RelativeLayout
relView.setX(x_cord - screenCenter + 40);
relView.setY(y_cord - 650);
switch (event.getAction()) {
//if picture slide down, nothing happens
case MotionEvent.ACTION_DOWN:
break;
//if pictures slide to side right, set rotation coordinates
//to RelativeLayout, set likes to 2
case MotionEvent.ACTION_MOVE:
x_cord = (int) event.getRawX();
y_cord = (int) event.getRawY();
relView.setX(x_cord - screenCenter + 40);
relView.setY(y_cord - 650);
if (x_cord >= screenCenter) {
relView.setRotation((float) ((x_cord - screenCenter) * (Math.PI / 32)));
if (x_cord > (screenCenter + (screenCenter / 2))) {
imageLike.setAlpha(1);
if (x_cord > (windowWidth - (screenCenter / 4))) {
likes = 2;
} else {
likes = 0;
}
} else {
likes = 0;
imageLike.setAlpha(0);
}
imagePass.setAlpha(0);
} else {
//Rotation
relView.setRotation((float) ((x_cord - screenCenter) * (Math.PI / 32)));
if (x_cord < (screenCenter / 2)) {
imagePass.setAlpha(1);
if (x_cord < screenCenter / 4) {
likes = 1;
} else {
likes = 0;
}
} else {
likes = 0;
imagePass.setAlpha(0);
}
imageLike.setAlpha(0);
}
break;
//if pictures moved up, picture were dropped to the primary position
case MotionEvent.ACTION_UP:
x_cord = (int) event.getRawX();
y_cord = (int) event.getRawY();
Log.e("X Point", "" + x_cord + " , Y " + y_cord);
imagePass.setAlpha(0);
imageLike.setAlpha(0);
//if like 0 nothing happens
if (likes == 0) {
Log.e("Event Status", "Nothing");
relView.setX(40);
relView.setY(40);
relView.setRotation(0);
//if like == 1 destroy RelativeLayout with the picture
} else if (likes == 1) {
Log.e("Event Status", "Passed");
imgContainer.removeView(relView);
} else if (likes == 2) {
//check and creat list
if (likeImageList == null) {
likeImageList = new ArrayList<>();
}
//add picture to list
LikedImage likedImage = new LikedImage();
likedImage.img = (BitmapFactory.decodeResource(getResources(), imageList[finalI]));
//save image as file
saveImageToExternalStorage(likedImage.img);
likeImageList.add(likedImage);
Log.e("Event Status", "Liked");
Log.e("Groeße der Liste", "" + likeImageList.size());
imgContainer.removeView(relView);
}
break;
default:
break;
}
return true;
}
});
imgContainer.addView(relView);
}
答案 0 :(得分:1)
使用Picaso ..
compile 'com.squareup.picasso:picasso:2.5.2'
你的年级
{{1}}
答案 1 :(得分:0)
您必须先下载图片。
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
然后使用Imageview.setImageBitmap将位图设置为ImageView。
答案 2 :(得分:0)
它很简单,你可以使用像glide这样的lazyload capacity的图像加载器:
首先将依赖项添加到build.gradle
dependencies {
// glide
compile 'com.github.bumptech.glide:glide:3.7.0'
}
然后在您的活动中使用它:
String imgUrl = "http://yoururl.com";
ImageView imageView = (ImageView) view.findViewById(R.id.thumbnail);
Glide.with(mContext).load(imgUrl)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
古德勒克。