Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package com.example.downloadingimageandpuingrid;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.widget.ImageView;
  9.  
  10. public class MainActivity extends ActionBarActivity {
  11.  
  12. public String url;
  13. public Byte [] imageBytes;
  14. public ImageView imageView;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.gallery_item);
  20. imageView = (ImageView) findViewById(R.layout.gallery_item);
  21. url = "http://animalia-life.com/odin/Cat/cat5.html";
  22. new FetchItemsTask().execute();
  23. }
  24.  
  25.  
  26. private class FetchItemsTask extends AsyncTask<Void,Void, byte[] > {
  27. @Override
  28. protected byte[] doInBackground(Void... params) {
  29. return new UrlDownload().getUrlBytes(url);
  30. }
  31.  
  32. @Override
  33. protected void onPostExecute(byte[] imageBytes) {
  34. final Bitmap bitmap = BitmapFactory
  35. .decodeByteArray(imageBytes, 0, imageBytes.length);
  36. imageView.setImageBitmap(bitmap);
  37.  
  38. }
  39. }
  40. }
  41.  
  42. package com.example.downloadingimageandpuingrid;
  43.  
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.net.HttpURLConnection;
  48. import java.net.URL;
  49.  
  50. import android.util.Log;
  51.  
  52. public class UrlDownload {
  53. byte[] getUrlBytes(String urlSpec)
  54. {
  55. try{
  56. return download(urlSpec);
  57. } catch(Exception e){
  58. //e.printStackTrace();
  59. Log.e("UrlDownload", "error downloading");
  60. byte[] empety = {};
  61. return empety;
  62. }
  63.  
  64. }
  65.  
  66. public byte[] download (String urlSpec) throws IOException
  67. {
  68. URL url = new URL(urlSpec);
  69. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  70. try {
  71. ByteArrayOutputStream out = new ByteArrayOutputStream();
  72. InputStream in = connection.getInputStream();
  73. if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
  74. {
  75. return null;
  76. }
  77. int bytesRead = 0;
  78. byte[] buffer = new byte[1024];
  79. while ((bytesRead = in.read(buffer)) > 0)
  80. {
  81. out.write(buffer, 0, bytesRead);
  82. }
  83. out.close();
  84. return out.toByteArray();
  85. }
  86. finally {
  87. connection.disconnect();
  88. }
  89. }
  90. }
  91.  
  92. <?xml version="1.0" encoding="utf-8"?>
  93. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
  94. android:id="@+id/gallery_item_imageView"
  95. android:layout_width="match_parent"
  96. android:layout_height="120dp"
  97. android:layout_gravity="center"
  98. android:scaleType="centerCrop" >
  99.  
  100. </ImageView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement