Guest User

Untitled

a guest
Jan 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. final ImageView v = (ImageView) findViewById(R.id.image);
  2. v.setImageResource(R.drawable.photo);
  3. v.invalidate();
  4.  
  5. final String[] params = new String[] {
  6. "",
  7. };
  8.  
  9. (new AsyncTask<String, Bitmap, Bitmap>()
  10. {
  11. @Override
  12. protected Bitmap doInBackground(final String... params)
  13. {
  14. Bitmap ret = null;
  15. for (final String url : params)
  16. {
  17. try
  18. {
  19. Log.e(TAG, url);
  20. ret = BitmapFactory.decodeStream((new URL(url)).openStream());
  21. publishProgress(ret);
  22. }
  23. catch (final MalformedURLException e)
  24. {
  25. Log.e(TAG, "Malformed URL", e);
  26. }
  27. catch (final IOException e)
  28. {
  29. Log.e(TAG, "IO Exception", e);
  30. }
  31. }
  32. return ret;
  33. }
  34.  
  35. @Override
  36. protected void onProgressUpdate(final Bitmap... values)
  37. {
  38. super.onProgressUpdate(values);
  39.  
  40. for (final Bitmap result : values)
  41. {
  42. if (result != null)
  43. {
  44. final ImageView v = (ImageView) MainActivity.this.findViewById(R.id.image);
  45. v.setImageBitmap(result);
  46. v.invalidate();
  47. }
  48. }
  49. }
  50. }).execute(params);
  51.  
  52. final WebView webview = (WebView) findViewById(R.id.webview);
  53. webview.loadData("<html><body><img src="" + url + ""></body></html>", "text/html", "utf-8");
  54. webview.invalidate();
  55.  
  56. final InputStream is = (new URL(url)).openStream();
  57. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  58. final int size = 1024;
  59. int len = -1;
  60. byte[] buf = new byte[size];
  61. while ((len = is.read(buf, 0, size)) != -1)
  62. {
  63. bos.write(buf, 0, len);
  64. }
  65. buf = bos.toByteArray();
  66.  
  67. // buf is now filled with the corrupted bytes of the image
  68.  
  69. ret = BitmapFactory.decodeByteArray(buf, 0, buf.length);
  70.  
  71. // ret is null because it was a corrupt jpg
  72.  
  73. private static final int JPEG_EOI_1 = 0xFF;
  74. private static final int JPEG_EOI_2 = 0xD9;
  75.  
  76. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  77. final int size = 1024;
  78. int len = -1;
  79. final byte[] buf = new byte[size];
  80. try
  81. {
  82. while ((len = is.read(buf, 0, size)) != -1)
  83. {
  84. bos.write(buf, 0, len);
  85. }
  86. bos.write(JPEG_EOI_1);
  87. bos.write(JPEG_EOI_2);
  88.  
  89. final byte[] bytes = bos.toByteArray();
  90.  
  91. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  92. }
  93. catch (final IOException ex)
  94. {
  95. return null;
  96. }
  97. catch (final Exception ex)
  98. {
  99. return null;
  100. }
  101.  
  102. return BitmapFactory.decodeStream(new JpegClosedInputStream(is));
  103.  
  104. // And here's the stream wrapper
  105. class JpegClosedInputStream extends InputStream
  106. {
  107. private final InputStream inputStream;
  108. private int bytesPastEnd;
  109.  
  110. private JpegClosedInputStream(final InputStream iInputStream)
  111. {
  112. inputStream = iInputStream;
  113. bytesPastEnd = 0;
  114. }
  115.  
  116. @Override
  117. public int read() throws IOException
  118. {
  119. int buffer = inputStream.read();
  120. if (buffer == -1)
  121. {
  122. if (bytesPastEnd > 0)
  123. {
  124. buffer = JPEG_EOI_2;
  125. }
  126. else
  127. {
  128. ++bytesPastEnd;
  129. buffer = JPEG_EOI_1;
  130. }
  131. }
  132.  
  133. return buffer;
  134. }
  135. }
  136.  
  137. //photo.xml
  138. <ImageView
  139. android:id="@+id/picture"
  140. android:layout_width="250dp"
  141. android:layout_height="250dp"
  142. android:layout_gravity="center"
  143. android:src="@drawable/photo" />
  144. />
  145. //Photo.java
  146. setContentView(R.layout.photo);
Add Comment
Please, Sign In to add comment