gpt4 book ai didi

android - 如何在android中的 ImageView 中显示我的所有图像

转载 作者:行者123 更新时间:2023-11-30 01:57:23 25 4
gpt4 key购买 nike

我的代码如下:

public class MainActivity extends Activity {
ImageView[] targetImage = new ImageView[5];
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection)) {
throw new IOException("Not an HTTP connection");
}
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex) {
Log.d("Networking", ex.getLocalizedMessage());
throw new IOException("Error connecting");
}

return in;
}

private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
Log.d("NetworkingActivity", e1.getLocalizedMessage());
}

return bitmap;
}

private class DownloadImageTask extends AsyncTask<String, Bitmap, Long> {
//---takes in a list of image URLs in String type---
protected Long doInBackground(String... urls) {
long imagesCount = 0;
for ( int i = 0; i < urls.length; i++) {
//---download the image---
Bitmap imageDownloaded = DownloadImage(urls[i]);
if (imageDownloaded != null ) {
//---increment the image count---
imagesCount++;
try {
//---insert a delay of 3 seconds---
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

//---return the image downloaded---
publishProgress(imageDownloaded);
}
}

//---return the total images downloaded count---
return imagesCount;
}

//---display the image downloaded---
protected void onProgressUpdate(Bitmap... bitmap) {
if(bitmap.length > 0) {
for(int i = 0; i < bitmap.length; i++) {
targetImage[i].setImageBitmap(bitmap[i]);
}
}
// tIV[values[i]].setImageBitmap(tBM[values[i]]);
}

//---when all the images have been downloaded---
protected void onPostExecute(Long imagesDownloaded) {
Toast.makeText(getBaseContext(),
"Total" + imagesDownloaded + " images downloaded" ,
Toast.LENGTH_LONG).show();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
targetImage[0] = (ImageView)findViewById(R.id.target0);
targetImage[1] = (ImageView)findViewById(R.id.target1);
targetImage[2] = (ImageView)findViewById(R.id.target2);

new DownloadImageTask().execute(
"http://solutionboat.com/work_2/asset/images/sobin.jpg" ,
"http://solutionboat.com/work_2/asset/images/shamol.jpg",
"http://solutionboat.com/work_2/asset/images/rifat.jpg"
);

ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
flipper.startFlipping();
}
}

最佳答案

在您的项目中导入毕加索库:

http://square.github.io/picasso/

然后试试这个:

    public class MainActivity extends Activity {
ImageView[] targetImage = new ImageView[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
targetImage[0] = (ImageView)findViewById(R.id.target0);
targetImage[1] = (ImageView)findViewById(R.id.target1);
targetImage[2] = (ImageView)findViewById(R.id.target2);
Picasso.with(MainActivity.this).load("http://solutionboat.com/work_2/asset/images/sobin.jpg").into(targetImage[0]);
Picasso.with(MainActivity.this).load("http://solutionboat.com/work_2/asset/images/shamol.jpg").into(targetImage[1]);
Picasso.with(MainActivity.this).load("http://solutionboat.com/work_2/asset/images/rifat.jpg").into(targetImage[2]);
}
}

关于android - 如何在android中的 ImageView 中显示我的所有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32018789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com