Convert Query Cursor Data to URL's and Run it in AsyncTask
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
add a comment |
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
ArrayList<URL> listURL = new ArrayList<>();pass yourArrayListto yourAsyncTask::private class DownloadTask extends AsyncTask<ArrayList<URL>, Integer, ArrayList<image_class>> {::: But do you really think you need the methodstringToURL()??
– Barns
Jan 3 at 1:10
u mean i will create anArrayList<URL> listURL = new ArrayList<>()then insert the links fromCursor?
– myown email
Jan 3 at 1:16
if i will pass it insideArrayListmaybe I wont needstringToURLand also can u provide me some sample?
– myown email
Jan 3 at 1:18
@Barns done creating arraylist how can I pass it?
– myown email
Jan 3 at 1:50
add a comment |
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
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
edited Jan 3 at 5:19
myown email
asked Jan 2 at 23:40
myown emailmyown email
499
499
ArrayList<URL> listURL = new ArrayList<>();pass yourArrayListto yourAsyncTask::private class DownloadTask extends AsyncTask<ArrayList<URL>, Integer, ArrayList<image_class>> {::: But do you really think you need the methodstringToURL()??
– Barns
Jan 3 at 1:10
u mean i will create anArrayList<URL> listURL = new ArrayList<>()then insert the links fromCursor?
– myown email
Jan 3 at 1:16
if i will pass it insideArrayListmaybe I wont needstringToURLand also can u provide me some sample?
– myown email
Jan 3 at 1:18
@Barns done creating arraylist how can I pass it?
– myown email
Jan 3 at 1:50
add a comment |
ArrayList<URL> listURL = new ArrayList<>();pass yourArrayListto yourAsyncTask::private class DownloadTask extends AsyncTask<ArrayList<URL>, Integer, ArrayList<image_class>> {::: But do you really think you need the methodstringToURL()??
– Barns
Jan 3 at 1:10
u mean i will create anArrayList<URL> listURL = new ArrayList<>()then insert the links fromCursor?
– myown email
Jan 3 at 1:16
if i will pass it insideArrayListmaybe I wont needstringToURLand also can u provide me some sample?
– myown email
Jan 3 at 1:18
@Barns done creating arraylist how can I pass it?
– myown email
Jan 3 at 1:50
ArrayList<URL> listURL = new ArrayList<>(); pass your ArrayList to your AsyncTask :: private class DownloadTask extends AsyncTask<ArrayList<URL>, Integer, ArrayList<image_class>> { ::: But do you really think you need the method stringToURL()??– Barns
Jan 3 at 1:10
ArrayList<URL> listURL = new ArrayList<>(); pass your ArrayList to your AsyncTask :: private class DownloadTask extends AsyncTask<ArrayList<URL>, Integer, ArrayList<image_class>> { ::: But do you really think you need the method stringToURL()??– Barns
Jan 3 at 1:10
u mean i will create an
ArrayList<URL> listURL = new ArrayList<>() then insert the links from Cursor?– myown email
Jan 3 at 1:16
u mean i will create an
ArrayList<URL> listURL = new ArrayList<>() then insert the links from Cursor?– myown email
Jan 3 at 1:16
if i will pass it inside
ArrayList maybe I wont need stringToURL and also can u provide me some sample?– myown email
Jan 3 at 1:18
if i will pass it inside
ArrayList maybe I wont need stringToURL and also can u provide me some sample?– myown email
Jan 3 at 1:18
@Barns done creating arraylist how can I pass it?
– myown email
Jan 3 at 1:50
@Barns done creating arraylist how can I pass it?
– myown email
Jan 3 at 1:50
add a comment |
1 Answer
1
active
oldest
votes
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 ArrayListas 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) {
}
}
i will try this definitely Thnx sir :)
– myown email
Jan 3 at 2:17
i got an error on the <> ofgetMyURLFromDB
– myown email
Jan 3 at 3:50
The code forgetMyURLFromDB()is basically just a stub--you need to adapt it to your code. The important thing is you usewhile(cursor.moveToNext()){to fill theArrayListwith the data from your database. You might be getting an error because a string value might not generate a validURL. But without any further information I can't help you with the error.
– Barns
Jan 3 at 4:56
for me this is one of the best pattern i can follow. tysm and i will accept. I will update you later on. this is too much
– myown email
Jan 3 at 4:58
Just send me a comment if you have any questions, no worries! Be sure to specify the type of error if you get an error.
– Barns
Jan 3 at 5:09
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014614%2fconvert-query-cursor-data-to-urls-and-run-it-in-asynctask%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 ArrayListas 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) {
}
}
i will try this definitely Thnx sir :)
– myown email
Jan 3 at 2:17
i got an error on the <> ofgetMyURLFromDB
– myown email
Jan 3 at 3:50
The code forgetMyURLFromDB()is basically just a stub--you need to adapt it to your code. The important thing is you usewhile(cursor.moveToNext()){to fill theArrayListwith the data from your database. You might be getting an error because a string value might not generate a validURL. But without any further information I can't help you with the error.
– Barns
Jan 3 at 4:56
for me this is one of the best pattern i can follow. tysm and i will accept. I will update you later on. this is too much
– myown email
Jan 3 at 4:58
Just send me a comment if you have any questions, no worries! Be sure to specify the type of error if you get an error.
– Barns
Jan 3 at 5:09
|
show 6 more comments
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 ArrayListas 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) {
}
}
i will try this definitely Thnx sir :)
– myown email
Jan 3 at 2:17
i got an error on the <> ofgetMyURLFromDB
– myown email
Jan 3 at 3:50
The code forgetMyURLFromDB()is basically just a stub--you need to adapt it to your code. The important thing is you usewhile(cursor.moveToNext()){to fill theArrayListwith the data from your database. You might be getting an error because a string value might not generate a validURL. But without any further information I can't help you with the error.
– Barns
Jan 3 at 4:56
for me this is one of the best pattern i can follow. tysm and i will accept. I will update you later on. this is too much
– myown email
Jan 3 at 4:58
Just send me a comment if you have any questions, no worries! Be sure to specify the type of error if you get an error.
– Barns
Jan 3 at 5:09
|
show 6 more comments
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 ArrayListas 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) {
}
}
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 ArrayListas 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 Jan 3 at 2:09
BarnsBarns
3,5203826
3,5203826
i will try this definitely Thnx sir :)
– myown email
Jan 3 at 2:17
i got an error on the <> ofgetMyURLFromDB
– myown email
Jan 3 at 3:50
The code forgetMyURLFromDB()is basically just a stub--you need to adapt it to your code. The important thing is you usewhile(cursor.moveToNext()){to fill theArrayListwith the data from your database. You might be getting an error because a string value might not generate a validURL. But without any further information I can't help you with the error.
– Barns
Jan 3 at 4:56
for me this is one of the best pattern i can follow. tysm and i will accept. I will update you later on. this is too much
– myown email
Jan 3 at 4:58
Just send me a comment if you have any questions, no worries! Be sure to specify the type of error if you get an error.
– Barns
Jan 3 at 5:09
|
show 6 more comments
i will try this definitely Thnx sir :)
– myown email
Jan 3 at 2:17
i got an error on the <> ofgetMyURLFromDB
– myown email
Jan 3 at 3:50
The code forgetMyURLFromDB()is basically just a stub--you need to adapt it to your code. The important thing is you usewhile(cursor.moveToNext()){to fill theArrayListwith the data from your database. You might be getting an error because a string value might not generate a validURL. But without any further information I can't help you with the error.
– Barns
Jan 3 at 4:56
for me this is one of the best pattern i can follow. tysm and i will accept. I will update you later on. this is too much
– myown email
Jan 3 at 4:58
Just send me a comment if you have any questions, no worries! Be sure to specify the type of error if you get an error.
– Barns
Jan 3 at 5:09
i will try this definitely Thnx sir :)
– myown email
Jan 3 at 2:17
i will try this definitely Thnx sir :)
– myown email
Jan 3 at 2:17
i got an error on the <> of
getMyURLFromDB– myown email
Jan 3 at 3:50
i got an error on the <> of
getMyURLFromDB– myown email
Jan 3 at 3:50
The code for
getMyURLFromDB() is basically just a stub--you need to adapt it to your code. The important thing is you use while(cursor.moveToNext()){ to fill the ArrayList with the data from your database. You might be getting an error because a string value might not generate a valid URL. But without any further information I can't help you with the error.– Barns
Jan 3 at 4:56
The code for
getMyURLFromDB() is basically just a stub--you need to adapt it to your code. The important thing is you use while(cursor.moveToNext()){ to fill the ArrayList with the data from your database. You might be getting an error because a string value might not generate a valid URL. But without any further information I can't help you with the error.– Barns
Jan 3 at 4:56
for me this is one of the best pattern i can follow. tysm and i will accept. I will update you later on. this is too much
– myown email
Jan 3 at 4:58
for me this is one of the best pattern i can follow. tysm and i will accept. I will update you later on. this is too much
– myown email
Jan 3 at 4:58
Just send me a comment if you have any questions, no worries! Be sure to specify the type of error if you get an error.
– Barns
Jan 3 at 5:09
Just send me a comment if you have any questions, no worries! Be sure to specify the type of error if you get an error.
– Barns
Jan 3 at 5:09
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014614%2fconvert-query-cursor-data-to-urls-and-run-it-in-asynctask%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
ArrayList<URL> listURL = new ArrayList<>();pass yourArrayListto yourAsyncTask::private class DownloadTask extends AsyncTask<ArrayList<URL>, Integer, ArrayList<image_class>> {::: But do you really think you need the methodstringToURL()??– Barns
Jan 3 at 1:10
u mean i will create an
ArrayList<URL> listURL = new ArrayList<>()then insert the links fromCursor?– myown email
Jan 3 at 1:16
if i will pass it inside
ArrayListmaybe I wont needstringToURLand also can u provide me some sample?– myown email
Jan 3 at 1:18
@Barns done creating arraylist how can I pass it?
– myown email
Jan 3 at 1:50