Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old December 28th, 2012, 02:12 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Dec 2012
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default asynctask to download images then load images into horizontal gallery

I am using asynctask to download images and then once it has finished downloading, it should load the images into my horizontal gallery. is it possible?

for now i have tried and the images are downloaded and saved but it doesnt show.

[HIGH]@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

new downloadFile().execute();
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));

gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Displaying the position when the gallery item in clicked
Toast.makeText(MainActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});

}

/**
* Background Async Task to Load all product by making HTTP Request
* */
class downloadFile extends AsyncTask<String, String, String> {

/**
* getting all magazines from url
* */
protected String doInBackground(String... args) {
URL myFileUrl = null;

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_login, "GET", params);

// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
mag = json.getJSONArray(TAG_MAGAZINE);

for (int i = 0; i < mag.length(); i++) {
JSONObject c = mag.getJSONObject(i);

// Storing each json item in variable
String magLink = c.getString(TAG_MAGAZINE_URL);
String magThumb = c.getString(TAG_MAGAZINE_THUMBNAIL);
//String magazineIssue = c.getString(TAG_MAGAZINE_ISSUE);

urlList.add(magLink);
//urlList.add(magazineIssue);
thumbnailList.add(magThumb);

System.out.println(thumbnailList);
}
}
else {

}
} catch (JSONException e) {
e.printStackTrace();
}


for (int i = 0; i < thumbnailList.size(); i ++)
{
thumbnail = thumbnailList.get(i).toString();
Log.d("thumbnail", thumbnail);
number = i;
try {
myFileUrl = new URL(thumbnail); // RETRIEVE IMAGE URL
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream in = conn.getInputStream();
Log.i("im connected", "Download");
bmImg = BitmapFactory.decodeStream(in);

File filename;
try {
// GET EXTERNAL STORAGE, SAVE FILE THERE
File storagePath = new File(Environment.getExternalStorageDirectory(),"Co vers");
storagePath.mkdirs();

filename = new File(storagePath + "/photo"+number+".jpg");
FileOutputStream out = new FileOutputStream(filename);
bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);

out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentReso lver(),filename.getAbsolutePath(), filename.getName(),
filename.getName());



// displayImage();
} catch (Exception e) {
e.printStackTrace();
}



} catch (IOException e) {
e.printStackTrace();
}

}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute() {

runOnUiThread(new Runnable() {
public void run() {

ArrayList<String> mStringList= new ArrayList<String>();
File strPath = new File(Environment.getExternalStorageDirectory() + "/Covers");
int lists = strPath.listFiles().length;
Log.d("number of items in array ",String.valueOf(lists));

File yourDir = new File(strPath, "");
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
String v = strPath + "/" + name;
mStringList.add(v);
}
}

mImageIds = new String[mStringList.size()];
mImageIds = mStringList.toArray(mImageIds);

for(int a = 0; a < mImageIds.length ; a++){
Log.d("string is",(mImageIds[a]));
}


}



});
}
}

public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;

public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_an droid_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return mImageIds.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView((Context) cont);

imgView.setImageDrawable(Drawable.createFromPath(m ImageIds[position]));

// Fixing width & height for image to display
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);

return imgView;
}
}

}[/HIGH]

j0lenee is offline  
Reply With Quote
sponsored links
Reply
Tags
android, image


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:54 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.