Here I am going to explain how to download Image From Url using AQuery .AQuery is an Asynchronous method to Download Images from url asynchronously.The AQuery is the Best way to save images which comes from a server in android.
Step 1:- Aquery Download
D link:https://github.com/androidquery/androidquery/releases/tag/0.26.8
Step 2:- MainActivity.java
package com.aquery;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.Date;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
public class MainActivity extends Activity
{
private AQuery aq;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView i = (ImageView) findViewById(R.id.img);
aq = new AQuery (this);
try
{
String url=" "; \\Paste the Url.
aq.id(R.id.img).progress(R.id.progress) .image(Url, true, true, 0, 0,new BitmapAjaxCallback()
{
@Override
public void callback(String url, ImageView iv,Bitmap bm, AjaxStatus status) {
iv.setImageBitmap(bm);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String EnString = Base64.encodeToString(b,Base64.DEFAULT);
File SDCardRoot =
Environment.getExternalStorageDirectory().getAbsoluteFile();
File dir =
new File(SDCardRoot.getAbsolutePath()+ "/Aquery" + "/Image");
//Download the Image In Sdcard
dir.mkdirs();
try
{
if (!SDCardRoot.equals(""))
{
String filename = "img"+ new Date().getTime() + ".png";
File file = new File(dir, filename);
if (file.createNewFile())
{
file.createNewFile();
}
if (EnString != null) {
FileOutputStream fos = new FileOutputStream(file);
byte[] decodedString = android.util.Base64.decode(EnString,
android.util.Base64.DEFAULT);
fos.write(decodedString);
fos.flush();
fos.close();
}
}
}
} catch (Exception e) {
}
}
});
} catch (Exception e) {
}
}
}
Step 3:- mainactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relative_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/default1"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" />
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_form_rounded"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Step 4:bg_gradient.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:gradientRadius="750"
android:endColor="#ffffff"
android:startColor="#fff000"
android:type="radial" />
</shape>
Step 5:bg_form_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Background color -->
<solid
android:color="#ffffff" >
</solid>
<!-- Padding -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<!-- Corner radius -->
<corners
android:radius="6dp" >
</corners>
</shape>
Step 6:greeting_default.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/default1"/>
</layer-list>
Step 7:Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Happy Coding:)
No comments:
Post a Comment