Vote count:
0
I've trying to download image and decode it to bitmap using BitmapFactory, but decodeStream always return null. I've googled many similar questions, tried many examples, but always the same.
Here is my code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void downloadButton(View v)
{
String imageUrl = "http://ift.tt/1jBSV0r";
new Thread(new ImageDownloader(imageUrl)).start();
}
public void showImageButton(View v)
{
Bitmap image = ImageHandler.getImage();
if (image == null)
Log.e("Error", "No image available");
else {
ImageView imageView = (ImageView)findViewById(R.id.ImageView);
imageView.setImageBitmap(image);
}
}
}
class ImageHandler
{
static protected List<Bitmap> imagesList;
static public void addImage(Bitmap image)
{
imagesList.add(image);
}
static public Bitmap getImage()
{
if (!imagesList.isEmpty())
return imagesList.get(0);
else
return null;
}
}
class ImageDownloader implements Runnable
{
public Bitmap bmpImage;
private String urlString;
public ImageDownloader(String urlString)
{
this.urlString = urlString;
}
public void run()
{
try
{
bmpImage = BitmapFactory.decodeStream(new URL(urlString).openStream());
}
catch (Exception e)
{
Log.e("ImageDownloadError", e.toString());
return;
}
if (bmpImage != null)
{
ImageHandler.addImage(bmpImage);
Log.i("Info", "Image download successfully");
}
else
Log.e("Error", "Bitmap is null");
}
}
p.s showImageButton throw IllegalStateException, but I was already sick of it.
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire