1 | package org.tipple.mapDisplay;
|
---|
2 |
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.OutputStream;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.net.URLConnection;
|
---|
11 |
|
---|
12 | import android.app.Activity;
|
---|
13 | import android.content.ContentValues;
|
---|
14 | import android.content.Context;
|
---|
15 | import android.graphics.Bitmap;
|
---|
16 | import android.graphics.BitmapFactory;
|
---|
17 | import android.graphics.Matrix;
|
---|
18 | import android.net.Uri;
|
---|
19 | import android.os.AsyncTask;
|
---|
20 | import android.os.Environment;
|
---|
21 | import android.provider.MediaStore;
|
---|
22 | import android.util.Log;
|
---|
23 |
|
---|
24 | public class ImageLoader extends AsyncTask<String, Integer, Bitmap> {
|
---|
25 |
|
---|
26 | private final String TAG = null;
|
---|
27 | public String myurl;
|
---|
28 | Context context;
|
---|
29 | String name;
|
---|
30 |
|
---|
31 | public ImageLoader(Activity context) {
|
---|
32 | this.context = context;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void download(String url) {
|
---|
36 | myurl = null;
|
---|
37 | myurl = url;
|
---|
38 | String[] imageName = myurl.split("\\/");
|
---|
39 | String finalName = imageName[11].replaceAll("%20", " ");
|
---|
40 | name = "/sdcard/" + finalName;
|
---|
41 | File file = new File(name);
|
---|
42 | if (file.exists()) {
|
---|
43 | System.out.println("File exists");
|
---|
44 | return;
|
---|
45 | } else
|
---|
46 | this.execute(myurl);
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected Bitmap doInBackground(String... params) {
|
---|
51 | return downloadAvatar(params[0]);
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | public Bitmap downloadAvatar(String url) {
|
---|
56 | Bitmap bm = null;
|
---|
57 | try {
|
---|
58 | URL aURL = new URL(url);
|
---|
59 | URLConnection conn = aURL.openConnection();
|
---|
60 | conn.connect();
|
---|
61 | InputStream is = conn.getInputStream();
|
---|
62 | BufferedInputStream bis = new BufferedInputStream(is);
|
---|
63 | bm = BitmapFactory.decodeStream(bis);
|
---|
64 | bis.close();
|
---|
65 | is.close();
|
---|
66 | }
|
---|
67 |
|
---|
68 | catch (Exception e) {
|
---|
69 | Log.w("AvatarDownloader", "Error while ...retrieving bitmap from "
|
---|
70 | + url);
|
---|
71 | }
|
---|
72 |
|
---|
73 | return bm;
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | String imageDirectory;
|
---|
78 |
|
---|
79 | protected void onPostExecute(Bitmap result) {
|
---|
80 | String rname = "/sdcard/" + myurl.toString().hashCode() + "-t.jpg";
|
---|
81 | try {
|
---|
82 | if (result != null) {
|
---|
83 | hasExternalStoragePublicPicture(name);
|
---|
84 | Bitmap rezised = this.resizeBitmap(result, 300, 300);
|
---|
85 | saveToSDCard(result, name, myurl.toString().hashCode() + ".jpg");
|
---|
86 | result.recycle();
|
---|
87 | rezised.recycle();
|
---|
88 |
|
---|
89 | } else {
|
---|
90 | System.out.println("onPostExecute... already exist");
|
---|
91 |
|
---|
92 | }
|
---|
93 | } catch (NullPointerException e) {
|
---|
94 | System.out.println("onPostExecute...unsuccessfully download ");
|
---|
95 | }
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | private boolean hasExternalStoragePublicPicture(String name) {
|
---|
100 | File file = new File(name);
|
---|
101 | if (file != null) {
|
---|
102 | file.delete();
|
---|
103 | }
|
---|
104 |
|
---|
105 | try {
|
---|
106 | file.createNewFile();
|
---|
107 |
|
---|
108 | } catch (IOException e) {
|
---|
109 | e.printStackTrace();
|
---|
110 |
|
---|
111 | }
|
---|
112 | return file.exists();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void saveToSDCard(Bitmap bitmap, String name, String nam) {
|
---|
116 | boolean mExternalStorageAvailable = false;
|
---|
117 | boolean mExternalStorageWriteable = false;
|
---|
118 | String state = Environment.getExternalStorageState();
|
---|
119 | if (Environment.MEDIA_MOUNTED.equals(state)) {
|
---|
120 | mExternalStorageAvailable = mExternalStorageWriteable = true;
|
---|
121 | Log.v(TAG, "SD Card is available for read and write "
|
---|
122 | + mExternalStorageAvailable + mExternalStorageWriteable);
|
---|
123 | saveFile(bitmap, name, nam);
|
---|
124 | } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
|
---|
125 | mExternalStorageAvailable = true;
|
---|
126 | mExternalStorageWriteable = false;
|
---|
127 | Log.v(TAG, "SD Card is available for read "
|
---|
128 | + mExternalStorageAvailable);
|
---|
129 | } else {
|
---|
130 | mExternalStorageAvailable = mExternalStorageWriteable = false;
|
---|
131 | Log.v(TAG, "Please insert a SD Card to save your Video "
|
---|
132 | + mExternalStorageAvailable + mExternalStorageWriteable);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void saveFile(Bitmap bitmap, String fullname, String nam) {
|
---|
137 |
|
---|
138 | ContentValues values = new ContentValues();
|
---|
139 |
|
---|
140 | File outputFile = new File(fullname);
|
---|
141 | values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
|
---|
142 | values.put(MediaStore.MediaColumns.TITLE, nam);
|
---|
143 | values.put(MediaStore.MediaColumns.DATE_ADDED,
|
---|
144 | System.currentTimeMillis());
|
---|
145 | values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
|
---|
146 | Uri uri = context.getContentResolver().insert(
|
---|
147 | android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
---|
148 |
|
---|
149 | values);
|
---|
150 | ;
|
---|
151 |
|
---|
152 | try {
|
---|
153 | OutputStream outStream = context.getContentResolver()
|
---|
154 | .openOutputStream(uri);
|
---|
155 | bitmap.compress(Bitmap.CompressFormat.JPEG, 95, outStream);
|
---|
156 |
|
---|
157 | outStream.flush();
|
---|
158 | outStream.close();
|
---|
159 | } catch (FileNotFoundException e) {
|
---|
160 | e.printStackTrace();
|
---|
161 | } catch (IOException e) {
|
---|
162 | e.printStackTrace();
|
---|
163 | }
|
---|
164 | bitmap.recycle();
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | public Bitmap resizeBitmap(Bitmap result, int i, int j) {
|
---|
169 | // TODO Auto-generated method stub
|
---|
170 | int width = result.getWidth();
|
---|
171 | int height = result.getHeight();
|
---|
172 | int newWidth = 1;
|
---|
173 | int newHeight = j;
|
---|
174 | float scaleWidth = ((float) newWidth) / width;
|
---|
175 | float scaleHeight = ((float) newHeight) / height;
|
---|
176 | Matrix matrix = new Matrix();
|
---|
177 | matrix.postScale(scaleWidth, scaleHeight);
|
---|
178 | // create the new Bitmap
|
---|
179 | Bitmap resizedBitmap = Bitmap.createBitmap(result, 0, 0, width, height,
|
---|
180 | matrix, true);
|
---|
181 | return resizedBitmap;
|
---|
182 | }
|
---|
183 | }
|
---|