Skip to content

Commit 51aa9dc

Browse files
committed
First image push
0 parents  commit 51aa9dc

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.loopj.android.image;
2+
3+
import java.io.InputStream;
4+
5+
import android.content.ContentUris;
6+
import android.content.ContentResolver;
7+
import android.provider.ContactsContract;
8+
import android.graphics.Bitmap;
9+
import android.graphics.BitmapFactory;
10+
import android.net.Uri;
11+
12+
public class ContactImage implements SmartImage {
13+
private static final int CONNECTION_TIMEOUT = 1000;
14+
15+
private String url;
16+
17+
public ContactImage(int contactId) {
18+
this.contactId = contactId;
19+
}
20+
21+
public Bitmap getBitmap() {
22+
Bitmap bitmap = null;
23+
24+
try {
25+
ContentResolver resolver = imageView.getContext().getContentResolver();
26+
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
27+
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
28+
if(input != null) {
29+
bitmap = BitmapFactory.decodeStream(input);
30+
}
31+
} catch(Exception e) {
32+
e.printStackTrace();
33+
}
34+
35+
return bitmap;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.loopj.android.image;
2+
3+
public interface SmartImage {
4+
public Bitmap getBitmap();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.loopj.android.image;
2+
3+
import android.os.Handler;
4+
import android.os.Message;
5+
6+
public class SmartImageTask implements Runnable {
7+
private static final int BITMAP_READY = 0;
8+
9+
private boolean cancelled = false;
10+
private OnCompleteHandler onCompleteHandler;
11+
12+
public static class OnCompleteHandler extends Handler {
13+
@Override
14+
public void handleMessage(Message msg) {
15+
Bitmap bitmap = (Bitmap)msg.obj;
16+
onComplete(bitmap);
17+
}
18+
19+
public void onComplete(Bitmap bitmap){};
20+
}
21+
22+
public void setOnCompleteHandler(OnCompleteHandler handler){
23+
this.onCompleteHandler = handler;
24+
}
25+
26+
public void cancel() {
27+
cancelled = true;
28+
}
29+
30+
public void complete(Bitmap bitmap){
31+
if(onCompleteHandler != null && !cancelled) {
32+
onCompleteHandler.sendMessage(onCompleteHandler.obtainMessage(BITMAP_READY, bitmap));
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.loopj.android.image;
2+
3+
import java.util.concurrent.Executors;
4+
import java.util.concurrent.ExecutorService;
5+
6+
public class SmartImageView extends ImageView {
7+
private static final int LOADING_THREADS = 4;
8+
private static ExecutorService threadPool = Executors.newFixedThreadPool(LOADING_THREADS);
9+
10+
private SmartImageTask currentTask;
11+
12+
public SmartImageView(Context context) {
13+
super(context);
14+
}
15+
16+
public SmartImageView(Context context, AttributeSet attrs) {
17+
super(context, attrs);
18+
}
19+
20+
public SmartImageView(Context context, AttributeSet attrs, int defStyle) {
21+
super(context, attrs, defStyle);
22+
}
23+
24+
public void setImageUrl(String url) {
25+
setImage(new UrlImage(url));
26+
}
27+
28+
public void setImageContact(int contactId) {
29+
setImage(new ContactImage(contactId));
30+
}
31+
32+
public void setImage(final SmartImage image) {
33+
setImage(image, null, null);
34+
}
35+
36+
public void setImage(final SmartImage image, final Integer fallbackResource) {
37+
setImage(image, fallbackResource, null);
38+
}
39+
40+
public void setImage(final SmartImage image, final Integer fallbackResource, final Integer loadingResource) {
41+
// Set a loading resource
42+
if(loadingResource != null && getDrawable() != null){
43+
setImageResource(loadingResource);
44+
}
45+
46+
// Cancel any existing tasks for this image view
47+
if(currentTask != null) {
48+
currentTask.cancel();
49+
currentTask = null;
50+
}
51+
52+
// Set up the new task
53+
currentTask = new SmartImageTask() {
54+
@Override
55+
public void run() {
56+
complete(image.getBitmap());
57+
}
58+
};
59+
60+
currentTask.setOnCompleteHandler(new SmartImageTask.OnCompleteHandler() {
61+
@Override
62+
public void onComplete(Bitmap bitmap) {
63+
if(bitmap != null) {
64+
setImageBitmap(bitmap);
65+
} else {
66+
// Set fallback resource
67+
if(fallbackResource != null) {
68+
setImageResource(fallbackResource);
69+
}
70+
}
71+
}
72+
});
73+
74+
// Run the task in a threadpool
75+
threadPool.execute(currentTask);
76+
}
77+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.loopj.android.image;
2+
3+
import java.io.InputStream;
4+
import java.net.URL;
5+
import java.net.URLConnection;
6+
7+
import android.graphics.Bitmap;
8+
import android.graphics.BitmapFactory;
9+
10+
public class UrlImage implements SmartImage {
11+
private static final int CONNECTION_TIMEOUT = 1000;
12+
13+
private String url;
14+
15+
public UrlImage(String url) {
16+
this.url = url;
17+
}
18+
19+
public Bitmap getBitmap() {
20+
Bitmap bitmap = null;
21+
22+
try {
23+
URLConnection conn = new URL(url).openConnection();
24+
conn.setConnectTimeout(1000);
25+
conn.setReadTimeout(1000);
26+
bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent());
27+
} catch(Exception e) {
28+
e.printStackTrace();
29+
}
30+
31+
return bitmap;
32+
}
33+
}

0 commit comments

Comments
 (0)