Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active June 16, 2016 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SelvinPL/175966ef024b4052c195fa7db34bdec4 to your computer and use it in GitHub Desktop.
Save SelvinPL/175966ef024b4052c195fa7db34bdec4 to your computer and use it in GitHub Desktop.
static volatile boolean isRunning = true;
void test() {
final int SIZE = 16 * 1024;
try {
final PipedOutputStream pipedOutputStream = new PipedOutputStream();
final PipedInputStream pipedInputStream = new PipedInputStream(SIZE);
pipedOutputStream.connect(pipedInputStream);
final Object wait = new Object();
new Thread(new Runnable() {
@Override
public void run() {
try {
Log.d("BitmapDecode", "starting!");
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 16;
//opts.inJustDecodeBounds = true;
final Bitmap bitmap = BitmapFactory.decodeStream(pipedInputStream, null, opts);
if(bitmap!=null) {
//bitmap is ready, use it here
Log.d("BitmapDecode", "bitmapObject: " + bitmap.getWidth() + "x" + bitmap.getHeight());
bitmap.recycle();
} else {
Log.d("BitmapDecode", "bitmap is null");
}
Log.d("BitmapDecode", "decodeBounds: " + opts.outWidth + "x" + opts.outHeight);
synchronized (wait) {
isRunning = false;
wait.notify();
}
} catch (Exception e) {
Log.e("BitmapDecode", "exception", e);
}
Log.d("BitmapDecode", "finished!");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
Log.d("BitmapDown", "starting!");
/* remove from here */
final URL url = new URL("http://selvin.pl/test4.jpg");
final URLConnection connection = url.openConnection();
final InputStream input = new BufferedInputStream(connection.getInputStream(), SIZE);
int len;
final byte[] buffer = new byte[SIZE];
while ((len = input.read(buffer, 0, SIZE)) > 0 && isRunning) {
Log.d("BitmapDown", "read: " + len);
pipedOutputStream.write(buffer, 0, len);
}
input.close();
/*until here and put your FTPClient code*/
pipedOutputStream.close();
synchronized (wait) {
if (isRunning) {
Log.d("BitmapDown", "about wait");
wait.wait();
}
}
pipedInputStream.close();
} catch (Exception e) {
Log.e("BitmapDown", "exception", e);
}
Log.d("BitmapDown", "finished!");
}
}).start();
} catch (Exception e) {
Log.e("Bitmap", "exception", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment