Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final ImageView v = (ImageView) findViewById(R.id.image);
- v.setImageResource(R.drawable.photo);
- v.invalidate();
- final String[] params = new String[] {
- "",
- };
- (new AsyncTask<String, Bitmap, Bitmap>()
- {
- @Override
- protected Bitmap doInBackground(final String... params)
- {
- Bitmap ret = null;
- for (final String url : params)
- {
- try
- {
- Log.e(TAG, url);
- ret = BitmapFactory.decodeStream((new URL(url)).openStream());
- publishProgress(ret);
- }
- catch (final MalformedURLException e)
- {
- Log.e(TAG, "Malformed URL", e);
- }
- catch (final IOException e)
- {
- Log.e(TAG, "IO Exception", e);
- }
- }
- return ret;
- }
- @Override
- protected void onProgressUpdate(final Bitmap... values)
- {
- super.onProgressUpdate(values);
- for (final Bitmap result : values)
- {
- if (result != null)
- {
- final ImageView v = (ImageView) MainActivity.this.findViewById(R.id.image);
- v.setImageBitmap(result);
- v.invalidate();
- }
- }
- }
- }).execute(params);
- final WebView webview = (WebView) findViewById(R.id.webview);
- webview.loadData("<html><body><img src="" + url + ""></body></html>", "text/html", "utf-8");
- webview.invalidate();
- final InputStream is = (new URL(url)).openStream();
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- final int size = 1024;
- int len = -1;
- byte[] buf = new byte[size];
- while ((len = is.read(buf, 0, size)) != -1)
- {
- bos.write(buf, 0, len);
- }
- buf = bos.toByteArray();
- // buf is now filled with the corrupted bytes of the image
- ret = BitmapFactory.decodeByteArray(buf, 0, buf.length);
- // ret is null because it was a corrupt jpg
- private static final int JPEG_EOI_1 = 0xFF;
- private static final int JPEG_EOI_2 = 0xD9;
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- final int size = 1024;
- int len = -1;
- final byte[] buf = new byte[size];
- try
- {
- while ((len = is.read(buf, 0, size)) != -1)
- {
- bos.write(buf, 0, len);
- }
- bos.write(JPEG_EOI_1);
- bos.write(JPEG_EOI_2);
- final byte[] bytes = bos.toByteArray();
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- }
- catch (final IOException ex)
- {
- return null;
- }
- catch (final Exception ex)
- {
- return null;
- }
- return BitmapFactory.decodeStream(new JpegClosedInputStream(is));
- // And here's the stream wrapper
- class JpegClosedInputStream extends InputStream
- {
- private final InputStream inputStream;
- private int bytesPastEnd;
- private JpegClosedInputStream(final InputStream iInputStream)
- {
- inputStream = iInputStream;
- bytesPastEnd = 0;
- }
- @Override
- public int read() throws IOException
- {
- int buffer = inputStream.read();
- if (buffer == -1)
- {
- if (bytesPastEnd > 0)
- {
- buffer = JPEG_EOI_2;
- }
- else
- {
- ++bytesPastEnd;
- buffer = JPEG_EOI_1;
- }
- }
- return buffer;
- }
- }
- //photo.xml
- <ImageView
- android:id="@+id/picture"
- android:layout_width="250dp"
- android:layout_height="250dp"
- android:layout_gravity="center"
- android:src="@drawable/photo" />
- />
- //Photo.java
- setContentView(R.layout.photo);
Add Comment
Please, Sign In to add comment