Issue
Hi I am using a function that looks like this. It converts a string to URL's
protected URL stringToURL(String urlString) {
try {
URL url = new URL(urlString);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
and sample use of that is this.
final URL url1 = stringToURL("www.link_to_my_page/sample_image1.jpg");
and if i want to use more i will just use it like this.
final URL url1 = stringToURL("www.link_to_my_page/sample_image1.jpg");
final URL url2 = stringToURL("www.link_to_my_page/sample_image2.jpg");
final URL url3 = stringToURL("www.link_to_my_page/sample_image3.jpg");
final URL url4 = stringToURL("www.link_to_my_page/sample_image4.jpg");
final URL url5 = stringToURL("www.link_to_my_page/sample_image5.jpg");
final URL url6 = stringToURL("www.link_to_my_page/sample_image6.jpg");
final URL url7 = stringToURL("www.link_to_my_page/sample_image7.jpg");
final URL url8 = stringToURL("www.link_to_my_page/sample_image8.jpg");
final URL url9 = stringToURL("www.link_to_my_page/sample_image9.jpg");
and then i have an AsyncTask
that will process all of that and here it is
private class DownloadTask extends AsyncTask<URL, Integer, ArrayList<image_class>> {
protected void onPreExecute() {
}
protected ArrayList<image_class> doInBackground(URL... urls) {
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onCancelled() {
}
protected void onPostExecute(ArrayList<image_class> result) {
}
}
and if i want to run my AsyncTask
i will call it like this
mMyTask = new DownloadTask().execute(url1);
or for multiple
mMyTask = new DownloadTask().execute(url1,url2,url3,url4,url5,url6,url7,url8,url9);
and now here is my concern. all my links where inside my SQLite
Database and what I did so far is create a Cursor
Loop them while using stringToURL
inside an array then call it like this mMyTask = new DownloadTask().execute(Array_of_Urls.ToString());
but no luck and it gave an errors.
My question is how can call that task using my urls in database?
Here is my current code
on Create Method
load_sync = findViewById(R.id.load_sync);
mProgressAnimation = new ProgressBarAnimation(load_sync, 100);
startMyAsyncTaskWithURList();
and here is the whole code
private ArrayList<URL> getMyURLFromDB() {
ArrayList<URL> listURL = new ArrayList<>();
Cursor cursor = Sync.get_image(current_email);
while (cursor.moveToNext()) {
String folder = cursor.getString(cursor.getColumnIndex("folder"));
String urlString = cursor.getString(cursor.getColumnIndex("myimage"));
String gen_link = "Custom URL" + folder + "/" + urlString;
Log.e("Link ", "" + gen_link);
URL url = null;
try {
url = new URL(gen_link);
} catch (MalformedURLException e) {
e.printStackTrace();
}
listURL.add(url);
}
return listURL;
}
private class DownloadTask extends AsyncTask<String, Integer, ArrayList<image_class>> {
protected void onPreExecute() {
}
ArrayList<URL> listURL = null;
public DownloadTask(ArrayList<URL> urls){
this.listURL = urls;
}
protected ArrayList<image_class> doInBackground(String... params) {
int count = listURL.size();
HttpURLConnection connection = null;
ArrayList<image_class> bitmaps = new ArrayList<>();
for (int i = 0; i < count; i++) {
URL currentURL = listURL.get(i);
String fileName = currentURL.toString().substring(currentURL.toString().lastIndexOf('/') + 1);
try {
connection = (HttpURLConnection) currentURL.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
bitmaps.add(new image_class(bmp,fileName));
publishProgress((int) (((i + 1) / (float) count) * 100));
if (isCancelled()) {
break;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return bitmaps;
}
protected void onProgressUpdate(Integer... progress) {
mProgressAnimation.setProgress(progress[0]);
}
protected void onPostExecute(ArrayList<image_class> result) {
Iterator itr = result.iterator();
while(itr.hasNext()){
image_class st = (image_class) itr.next();
Bitmap bitmap = st.image;
saveImageToInternalStorage(bitmap, st.filename);
}
}
}
protected Uri saveImageToInternalStorage(Bitmap bitmap, String filename) {
File file = sdCardDirectory;
file = new File(file, filename);
try {
OutputStream stream = null;
stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
Uri savedImageURI = Uri.parse(file.getAbsolutePath());
return savedImageURI;
}
the image doesnt download or the AsyncTask is not working
Solution
This is just a basic outline:
private ArrayList<> getMyURLFromDB(){
ArrayList<> listURL = new ArrayList<>();
// You will need to fill in some blanks here...
//... This is just a basic outline!
SQLiteDatabase db = dbModel.getReadableDatabase();
Cursor cursor = db....
while (cursor.moveToNext()) {
String urlString = cursor.getString(cursor.getColumnIndex(MY_URL_STRING));
URL url = new URL(urlString);
listURL.add(url);
}
return listURL;
}
Get the List from the database and pass the ArrayList
as a parameter of the AsyncTask
:
private void startMyAsyncTaskWithURList(){
ArrayList<URL> listURL = getMyURLFromDB();
// Just pass the ArrayList as a parameter
new DownloadTask(listURL).execute();
}
You need to add a constructor of the AsyncTask
that will accept the ArrayList
as a parameter:
private class DownloadTask extends AsyncTask<String, Integer, ArrayList<image_class>> {
ArrayList<URL> listURL = null;
public DownloadTask(ArrayList<URL> urls){
this.listURL = urls;
}
protected ArrayList<image_class> doInBackground(String... params) {
// Now you have access to the list
if(listURL != null){
}
}
protected void onPostExecute(ArrayList<image_class> result) {
}
}
Answered By - Barns
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.