tag:blogger.com,1999:blog-44719258281590080022023-11-15T08:57:24.398-08:00My NotesMy Notebook. Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-4471925828159008002.post-35959370052096796722012-04-09T05:44:00.000-07:002012-04-09T06:13:39.137-07:00Handle defaulthttpclient redirects manually<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;">By default</span> <span style="font-family: 'Times New Roman', serif; font-size: 12pt;"><a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html"><span style="color: blue; font-size: 13.5pt;">DefaultHttpClient</span></a></span><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"> will
handle redirect automatically. Sometimes we need to handle
this manually, mainly if we need to set some parameters with every
request.</span><br />
<span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"><br /></span><br />
<span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;">We can do it by the following steps.</span><br />
<ol start="1" type="1">
<li class="MsoNormal"><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;">Set </span><b><span style="font-family: 'Times New Roman', serif; font-size: 12pt;"><a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/params/ClientPNames.html#HANDLE_REDIRECTS"><span style="color: blue; text-decoration: none;">HANDLE_REDIRECTS</span></a></span></b><b><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"> </span></b><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;">parameter value to false , that means it </span><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"> </span><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;">defines redirects should be handled manually<o:p></o:p></span></li>
</ol>
<div style="background: #EEEEEE; border: dashed #999999 1.0pt; margin-left: .5in; margin-right: 0in; mso-border-alt: dashed #999999 .75pt; mso-element: para-border-div; padding: 4.0pt 4.0pt 4.0pt 4.0pt;">
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Courier New'; font-size: 10pt;">HttpParams params = httpClient.getParams();<o:p></o:p></span></div>
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Courier New'; font-size: 10pt;">HttpClientParams.setRedirecting(params, false);<o:p></o:p></span></div>
</div>
<ol start="2" type="1">
<li class="MsoNormal"><span style="font-family: 'Times New Roman', serif; font-size: medium;"><span style="font-size: 13.5pt;">Then after executing our
request, if it is not HttpStatus.SC_OK or when response codes between 300 and
399 ( redirect responses )</span></span></li>
<ul type="circle">
<li class="MsoNormal"><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;">Gets the values of the response header with the name
as "<i>Location</i>"<o:p></o:p></span></li>
<li class="MsoNormal"><span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;">the last value will be the new url where it need to
redirect <o:p></o:p></span></li>
<li class="MsoNormal"><span style="font-family: 'Times New Roman', serif;"><span style="font-size: medium;">then execute the new request using the
redirect url by setting the needed parameters<o:p></o:p></span></span></li>
</ul>
</ol>
<div style="background: #EEEEEE; border: dashed #999999 1.0pt; margin-left: .5in; margin-right: 0in; mso-border-alt: dashed #999999 .75pt; mso-element: para-border-div; padding: 4.0pt 4.0pt 4.0pt 4.0pt;">
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Lucida Console'; font-size: 9pt;">if (responseCode !=
HttpStatus.SC_OK) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Lucida Console'; font-size: 9pt;"> Header[] headers =
httpResponse.getHeaders("Location");<o:p></o:p></span></div>
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Lucida Console'; font-size: 9pt;"> if
(headers != null && headers.length != 0) {<o:p></o:p></span></div>
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Lucida Console'; font-size: 9pt;"> String newUrl = headers[headers.length -
1].getValue();<o:p></o:p></span></div>
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Lucida Console'; font-size: 9pt;"> //Execute again with newUrl by setting the needed
paramters <o:p></o:p></span></div>
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; line-height: 10.5pt; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Lucida Console'; font-size: 9pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<span style="font-family: 'Lucida Console'; font-size: 9pt;">}</span><span style="font-family: 'Courier New'; font-size: 10pt;"><o:p></o:p></span></div>
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br />
<div class="MsoNormal">
<span style="font-family: 'Times New Roman', serif; font-size: 13.5pt; line-height: 115%;">My
complete <i>doHttpRequest </i>method</span></div>
<div class="MsoNormal">
</div>
<div style="background: #EEEEEE; border: dashed #999999 1.0pt; margin-left: 0in; margin-right: 0in; mso-border-alt: dashed #999999 .75pt; mso-element: para-border-div; padding: 4.0pt 4.0pt 4.0pt 4.0pt;">
<div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0.0001pt; padding-bottom: 0in; padding-left: 0in; padding-right: 0in; padding-top: 0in;">
<br />
<div style="font-size: 12px; line-height: 14px;">
<span style="font-family: 'Lucida Console';"></span></div>
<span style="font-family: 'Lucida Console';">private void doHttpRequest(String url){</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>DefaultHttpClient httpClient = new DefaultHttpClient();</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>HttpPost request = new HttpPost(url);</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="font-family: 'Lucida Console';"> request.setParams(getHttpParams()); </span>
<br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>//getHttpParams is used to get the HttpParams </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>//by setting all the required parameters</span><span class="Apple-tab-span" style="font-family: 'Lucida Console'; white-space: pre;"> </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>//Set HANDLE_REDIRECTS to false , </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>//defines redirects should be handled manually</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>HttpParams params = httpClient.getParams();</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>HttpClientParams.setRedirecting(params, false);</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>HttpResponse httpResponse = </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>httpClient.execute(request);</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>int responseCode = </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span> httpResponse.getStatusLine().getStatusCode();</span><br />
<span style="font-family: 'Lucida Console';"><br /></span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>if (responseCode != HttpStatus.SC_OK) {</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>Header[] headers = </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>httpResponse.getHeaders("Location");</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>if (headers != null && headers.length != 0) {</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span> //Need to redirect </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span> String newUrl = </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span> headers[headers.length - 1].getValue();</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>doHttpRequest(newUrl);</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>//call the doHttpRequest </span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>//recursively with new url</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>} else {</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>handleResponse(httpResponse);</span><br />
<span style="font-family: 'Lucida Console';"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span style="font-family: 'Lucida Console';">}</span><br />
<br /></div>
</div>
<br />
<span style="font-family: monospace;">Hope this help.</span><br />
<span style="font-family: monospace;">And please feel free to point out my mistakes... </span>
</div>
<br /></div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com1tag:blogger.com,1999:blog-4471925828159008002.post-88891152809915638812011-11-05T05:02:00.000-07:002011-11-05T05:21:57.425-07:00Simple Content Provider for db operations<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
At last I concluded, it is <a href="http://developer.android.com/reference/android/content/ContentProvider.html">ContentProvider</a> which we need to use for database operation ..<br />
<blockquote class="tr_bq">
<b>Content Providers </b> </blockquote>
<blockquote class="tr_bq">
<b></b><span class="Apple-style-span" style="background-color: white; color: #333333; font-family: arial, sans-serif; font-size: 13px; line-height: 16px;">Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications;</span> </blockquote>
<br />
<ul style="text-align: left;">
<li>Yes content providers are mainly aimed to share data amount different application</li>
<li>It can be also used inside our single application</li>
<li>There is no worries of closing and locking of db</li>
<li>It can be accessed from different activities, services and Thread. Anywhere that have context object</li>
<li><i>I don't know is this a bad idea, </i>I don't think so. Don't think it will reduce our data security. Our data can be accessed only with our AUTHORITY url</li>
</ul>
<blockquote class="tr_bq">
<span class="Apple-style-span" style="background-color: white; color: #333333; font-family: arial, sans-serif; font-size: 13px; line-height: 16px;">When a request is made via a </span><code style="background-color: white; color: #007000; font-size: 13px; line-height: 1em; text-align: -webkit-auto;"><a href="http://developer.android.com/reference/android/content/ContentResolver.html" style="color: #006699;">ContentResolver</a></code><span class="Apple-style-span" style="background-color: white; color: #333333; font-family: arial, sans-serif; font-size: 13px; line-height: 16px; text-align: -webkit-auto;"> the system inspects the authority of the given URI and passes the request to the content provider registered with the authority.</span></blockquote>
Now I'm trying to demonstrate the way how we can use <a href="http://developer.android.com/reference/android/content/ContentProvider.html">ContentProvider</a> in most simple way...<br />
ContentProviderDb.java</div>
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
public class ContentProviderDb extends ContentProvider{
OurDatabaseHelper dbHelper ;
public static final String AUTHORITY = "ourContentProviderAuthorities";//specific for our our app, will be specified in maninfed
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
@Override
public boolean onCreate() {
dbHelper = new OurDatabaseHelper(getContext());
return true;
}
@Override
public int delete(Uri uri, String where, String[] args) {
String table = getTableName(uri);
SQLiteDatabase dataBase=dbHelper.getWritableDatabase();
return dataBase.delete(table, where, args);
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
String table = getTableName(uri);
SQLiteDatabase database = dbHelper.getWritableDatabase();
long value = database.insert(table, null, initialValues);
return Uri.withAppendedPath(CONTENT_URI, String.valueOf(value));
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String table =getTableName(uri);
SQLiteDatabase database = dbHelper.getReadableDatabase();
Cursor cursor =database.query(table, projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String whereClause,
String[] whereArgs) {
String table = getTableName(uri);
SQLiteDatabase database = dbHelper.getWritableDatabase();
return database.update(table, values, whereClause, whereArgs);
}
public static String getTableName(Uri uri){
String value = uri.getPath();
value = value.replace("/", "");//we need to remove '/'
return value;
}
}
</code></pre>
</div>
<br />
Here <span class="Apple-style-span" style="background-color: #eeeeee; font-family: monospace; font-size: 12px; line-height: 14px; white-space: pre;">OurDatabaseHelper</span> is the database helper class (extends SQLiteOpenHelper )<br />
<span class="Apple-style-span" style="background-color: #eeeeee; font-family: monospace; font-size: 12px; line-height: 14px; white-space: pre;">getTableName</span> is used to get the specific Table name<br />
<br />
Don't forget to add this in your <i>manifest.xml</i> file<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code><provider android:authorities="ourContentProviderAuthorities"
android:name=".ContentProviderDb"></code></pre>
<code>We specify our </code><span class="Apple-style-span" style="background-color: #eeeeee; font-family: monospace; font-size: 12px; line-height: 14px; white-space: pre;">AUTHORITY</span><span class="Apple-style-span" style="font-family: monospace;"> as </span><span class="Apple-style-span" style="background-color: #eeeeee; font-family: monospace; font-size: 12px; line-height: 14px; white-space: pre;">android:authorities</span><br />
<code>
<br />
So the db operation will be like this:<br />
</code><br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>Uri contentUri = Uri.withAppendedPath(ContentProviderDb.CONTENT_URI, "TableName");</code></pre>
<code>
Using these contentUri we are specifying our table where we need to perform our operation<br />
<br />
We can perform db operation using our context</code></div>
<code>
</code></div>
<code>
</code><br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code><code>context.getContentResolver().insert(.....);
context.getContentResolver().query(.....);
context.getContentResolver().delete(.....);
context.getContentResolver().update(.....);
</code></code></pre>
<code>
</code></div>
<code><br /></code><br />
<code>
And if you are inside an activity class<br />
</code><br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code><code>getContentResolver().insert(.....);
getContentResolver().query(.....);
getContentResolver().delete(.....);
getContentResolver().update(.....);
</code></code></pre>
<code>
<br />
For an example an insert operation will look like this</code></div>
<code>
</code><br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>ContentValues initialValues = new ContentValues();
initialValues.put("Column", "value");
Uri contentUri = Uri.withAppendedPath(ContentProviderDb.CONTENT_URI, "TableName");
Uri resultUri = context.getContentResolver().insert(contentUri, initialValues);
</code></pre>
<code>
<br />
Hope this help.<br />
And please feel free to point out my mistakes... </code></div>
<code>
</code></div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com8tag:blogger.com,1999:blog-4471925828159008002.post-61300237796543022222011-09-25T23:25:00.000-07:002011-09-25T23:26:53.028-07:00Sending Multiple file attached email in android<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Sending Multiple file attached email in android ,<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
public boolean sendEmailWithMultipleAttachments(Context context,
String[] emailTo, String[] emailCC, String[] emailBCC,
String subject, String emailBody, List<string> filePaths) throws ActivityNotFoundException {
final Intent emailIntent =
new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailTo);
emailIntent.putExtra(android.content.Intent.EXTRA_CC, emailCC);
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, emailBCC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
if (filePaths != null) {
// has to be an ArrayList
ArrayList<uri> uris = new ArrayList<uri>();
// convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths) {
File fileIn = new File(file);
if (fileIn.exists()) {
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
}
emailIntent.putParcelableArrayListExtra
(Intent.EXTRA_STREAM, uris);
}
context.startActivity(Intent.createChooser(emailIntent, "Sent mail"));
return true;
}
</uri></uri></string></code></pre>
</div>
<br />
<span class="Apple-style-span" style="background-color: #eeeeee; font-family: monospace; font-size: 12px; line-height: 14px; white-space: pre;">filePaths</span> is list, which store the local path of file which we need to attach<br />
eg<br />
<span class="Apple-style-span" style="background-color: #eeeeee; font-family: monospace; font-size: 12px; line-height: 14px; white-space: pre;">filePaths.add("/sdcard/test.pdf");</span></div>
Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com0tag:blogger.com,1999:blog-4471925828159008002.post-23257123942758257722011-09-25T22:24:00.000-07:002011-09-25T22:26:14.099-07:00Get mime type of a file<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
In android to get mime type is very easy...<br />
<br />
To find the mime type of a file, path
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
String extention = path.substring(path.lastIndexOf(.) );
String mimeTypeMap =MimeTypeMap.getFileExtensionFromUrl(extention);
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(mimeTypeMap);
</code></pre>
</div>
Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com2tag:blogger.com,1999:blog-4471925828159008002.post-30457463902291879642011-04-27T22:05:00.000-07:002011-09-25T22:27:58.753-07:00Create war file in grails<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on">The <code>war</code> command will create a Web Application Archive (WAR) file which can be deployed on any Java EE compliant application server<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
grails war
grails test war
grails -Dgrails.env=foo war
</code></pre><br />
<pre><b>Usage: </b></pre><pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
grails [environment]* war [arguments]*
</code></pre><div class="paragraph">Arguments: </div><ul class="star"><li><code>nojars</code> - Packages the WAR with no jar files. Used for shared deployment</li>
</ul><div class="paragraph">Fired Events: </div><ul class="star"><li><code>StatusFinal</code> - When the WAR file has been created</li>
</ul>The war command will create a web application archive (WAR) file using the application name and version number. By default a war file deployable to the production environment will be created, but the environment can be specified using Grails standard convention <br />
<ul class="star"></ul><pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
grails test war
grails dev war
grails prod war
grails war /foo/bar/mywar.war
grails war --nojars
</code></pre></div><br />
Always better to call <br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
grails clean
</code></pre>The clean command will delete all compiled resources from the current Grails application. Since Groovy is a compiled language, as with Java, this is sometimes useful to clear old instances of classes out and ensure correct compilation<br />
<br />
Refer <a href="http://grails.org/doc/latest/ref/Command%20Line/war.html">war</a> </div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com0tag:blogger.com,1999:blog-4471925828159008002.post-33302454090633225302011-04-16T07:23:00.000-07:002011-09-25T22:27:12.158-07:00Using Fragment of Android Compatibility Package<div dir="ltr" style="text-align: left;" trbidi="on">The Android 3.0 introduce a awesome feature <a href="http://developer.android.com/guide/topics/fundamentals/fragments.html">Fragment</a>.<br />
It really helpful when we design for tablet. You can refer <a href="http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html">The Android 3.0 Fragment API</a> for more details<br />
Here I'm going to explain how to use Fragment for lower version.<br />
Now android had released a static library that exposes the same Fragments API, so that applications compatible with Android 1.6 or later can use fragments to create tablet-compatible user interfaces.<br />
<br />
For this you need to do follow these steps<br />
<ul style="text-align: left;"><li>Add <b>android-support-v4.jar</b> to you project</li>
<ul><li>You can found this jar at <i><code>yoursdkpath/extras/android/compatibility/v4/</code></i></li>
<li>Copy this into your libs folder in the root of your project </li>
<li>add this jar to your project build path </li>
</ul><li>In order to use this you need to use <b>FragmentActivity </b>instead of <b>Activity </b></li>
<ul><li><pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>public class Home extends FragmentActivity</code></pre></li>
</ul><li>To get FragmentManager </li>
<ul><li>Need to use <b>getSupportFragmentManager </b>instead of <b>getFragmentManage</b></li>
</ul></ul>The complete code will look like this<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class Home extends FragmentActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment productDetails = new MyFragment();
fragmentTransaction.replace(R.id.mainFragement, productDetails);
fragmentTransaction.commit();
}
}
</code></pre>MyFragment.java<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment{
public MyFragment(){
Log.i("MYEXPENSETRACKER","MyFragment ()");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView t = new TextView(getActivity());
t.setText("From Fragement");
return t;
}
}
</code></pre>The main.xml I used<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<FrameLayout
android:id="@+id/mainFragement"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
</code></pre>Hope it helped you..:)</div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com23tag:blogger.com,1999:blog-4471925828159008002.post-67727997807554212642011-04-13T08:14:00.000-07:002011-09-25T22:27:32.289-07:00Go back to previous activity<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on">Its every common query how to go back to previous activity.<br />
Most of us will finish activity or keep track of the activity stack and all.<br />
But no need of this type of complex logic if you need just the same effect of user's back key press <br />
You can use the android onBackPressed() method explicitly .<br />
<br />
</div><pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>super.onBackPressed();</code></pre><br />
The default implementation simply finishes the current activity, but you can override this to do whatever you want.<br />
The android implicitly call this method when the activity has detected the user's press of the back key.<br />
So calling this explicitly will give the same effect </div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com10tag:blogger.com,1999:blog-4471925828159008002.post-62462436196264361402011-03-28T05:29:00.000-07:002011-03-28T05:29:40.082-07:00In Android import xml into another xml<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on">Its very common in the UI design that some common element appear many time. <br />
Just like some common signature in all the activities (pages) and all. <br />
Its common that we need to set some component as reusable component.<br />
<br />
In this case we can use the <code><include /></code> tag<include>. This element does exactly what its name suggest.<br />
To use this is very simple<br />
Declare the reusable component as one layout in the <i>/res/layout</i>, let say <i>commonlayout.xml</i><br />
Then where ever you need to refer this you can use the following code </include></div><pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code><include android:id="@+id/id" layout="@layout/commonlayout"/>
</code></pre></div>Note that:<br />
<ul style="text-align: left;"><li>Here you can override all the layout parameters. This means that any <code>android:layout_*</code> attribute can be used with the <code><include /></code> tag</li>
<li> If you want to override the layout dimensions, you must override both <code>android:layout_height</code> and <code>android:layout_width</code> you cannot override only the height or only the width. If you override only one, it will not take effect. (Other layout properties, such as weight, are still inherited from the source layout.) </li>
</ul>Refer <a href="http://developer.android.com/resources/articles/layout-tricks-reuse.html">Creating Reusable UI Components</a><br />
<br />
<br />
<br />
</div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com3tag:blogger.com,1999:blog-4471925828159008002.post-76050929201087868152011-03-21T23:10:00.000-07:002011-03-21T23:10:14.306-07:00Pinch Zoom to view completely<div dir="ltr" style="text-align: left;" trbidi="on">Hope you have gone through my previous post <a href="http://myandroidnote.blogspot.com/2011/03/zooming-view-completely.html">Zooming a view completely </a><br />
Now to apply pinch zoom we need to override the OnTouchListener.<br />
There are many blogs and code to do pinch zoom in image view me used that logic here<br />
So my main activity class will be like this<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
public class PinchZoomLayout extends Activity {
View mainView = null;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
PointF oldDistPoint = new PointF();
public static String TAG = "ZOOM";
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView = (LinearLayout) findViewById(R.id.linearLayout);
Button buttonZoomOut = (Button) findViewById(R.id.buttonZoomOut);
Button buttonNormal = (Button) findViewById(R.id.buttonNormal);
Button buttonZoomIn = (Button) findViewById(R.id.buttonZoomIn);
buttonZoomOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(0.5f, 0.5f, new PointF(0, 0));
}
});
buttonNormal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(1f, 1f, new PointF(0, 0));
}
});
buttonZoomIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(2f, 2f, new PointF(0, 0));
}
});
mainView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "mode=DRAG");
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
oldDistPoint = spacingPoint(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
System.out.println("current time :" + System.currentTimeMillis());
break;// return !gestureDetector.onTouchEvent(event);
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
Log.d(TAG, "mode=NONE");
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
} else if (mode == ZOOM) {
PointF newDist = spacingPoint(event);
float newD = spacing(event);
Log.e(TAG, "newDist=" + newDist);
float[] old = new float[9];
float[] newm = new float[9];
Log.e(TAG, "x=" + old[0] + ":&:" + old[2]);
Log.e(TAG, "y=" + old[4] + ":&:" + old[5]);
float scale = newD / oldDist;
float scalex = newDist.x / oldDistPoint.x;
float scaley = newDist.y / oldDistPoint.y;
zoom(scale, scale, start);
}
break;
}
return true;
}
});
}
/**
* zooming is done from here
*/
public void zoom(Float scaleX, Float scaleY, PointF pivot) {
mainView.setPivotX(pivot.x);
mainView.setPivotY(pivot.y);
mainView.setScaleX(scaleX);
mainView.setScaleY(scaleY);
}
/**
* space between the first two fingers
*/
private float spacing(MotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private PointF spacingPoint(MotionEvent event) {
PointF f = new PointF();
f.x = event.getX(0) - event.getX(1);
f.y = event.getY(0) - event.getY(1);
return f;
}
/**
* the mid point of the first two fingers
*/
private void midPoint(PointF point, MotionEvent event) {
// ...
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
</code> </pre>But this zoom is not much smooth :(.<br />
</div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com18tag:blogger.com,1999:blog-4471925828159008002.post-6549017753276500992011-03-21T22:54:00.000-07:002011-03-21T23:01:57.473-07:00Zooming a view completely<div dir="ltr" style="text-align: left;" trbidi="on">Here I'm discussing how to zoom a complete view with different UI controls in it.<br />
For this i used setScaleX and setScaleY.<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
mainView.setScaleX(scaleX);
mainView.setScaleY(scaleY);
</code> </pre>This will Scale the the view in X and Y axis. A value of scale 1 means that no scaling is applied.<br />
In order to work this correctly it need to specify the pivot point correctly. We can set our pivot point using setPivotX and setPivotY.<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
mainView.setPivotX(pivot.x);
mainView.setPivotY(pivot.y);
</code> </pre>My complete code <br />
<br />
Main.xml<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<Button
android:text="Zoom In"
android:id="@+id/buttonZoomIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Normal"
android:id="@+id/buttonNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Zoom Out"
android:id="@+id/buttonZoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:src="@drawable/honeycombdroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="200dp"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<EditText
android:id="@+id/editText1"
android:layout_height="wrap_content"
android:text="EditText"
android:layout_width="match_parent"></EditText>
<EditText
android:id="@+id/editText1"
android:layout_height="wrap_content"
android:text="EditText"
android:layout_width="match_parent"></EditText>
<Button
android:text="Button"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
</code> </pre><br />
Here me only use that three buttons <br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>buttonZoomIn , </code><code>buttonNormal and </code><code>buttonZoomOut</code></pre>Then My main activity class<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
public class ZoomLayout extends Activity {
View mainView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView =(LinearLayout)findViewById(R.id.linearLayout);
Button buttonZoomOut = (Button)findViewById(R.id.buttonZoomOut);
Button buttonNormal = (Button)findViewById(R.id.buttonNormal);
Button buttonZoomIn = (Button)findViewById(R.id.buttonZoomIn);
buttonZoomOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(0.5f,0.5f,new PointF(0,0));
}
});
buttonNormal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(1f,1f,new PointF(0,0));
}
});
buttonZoomIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoom(2f,2f,new PointF(0,0));
}
});
}
/** zooming is done from here */
public void zoom(Float scaleX,Float scaleY,PointF pivot){
mainView.setPivotX(pivot.x);
mainView.setPivotY(pivot.y);
mainView.setScaleX(scaleX);
mainView.setScaleY(scaleY);
}
}
</code><code>buttonZoomOut</code></pre>Here zoom is the main function for zooming </div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com21tag:blogger.com,1999:blog-4471925828159008002.post-41523584661528386732011-03-18T06:15:00.000-07:002011-03-18T06:15:18.096-07:00Load Image from server using thread<div dir="ltr" style="text-align: left;" trbidi="on">This is a project where I implemented how to load image form server<br />
Here we have a image URL and need to load that image to a image view<br />
<br />
In the layout xml I defined a web view <br />
<br />
<div style="color: black;">main.xml </div><pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</code></pre><br />
Then in my activity i have used a thread (<i>ImageDownlaodThread</i>) to load the image from server. Where the function <i>LoadImageFromWebOperations </i>will read to image to <i>InputStream, </i>this is later converted to Drawable by <a href="http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromStream%28java.io.InputStream,%20java.lang.String%29">Drawable.createFromStream</a><br />
<br />
Using the <a href="http://developer.android.com/reference/android/os/Handler.html">Handler</a>, when the thread convert to drawable it will be set to the image view <br />
<br />
<br />
Then my main activity ImageDownload.java<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class ImageDownload extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView mainImageView = (ImageView) findViewById(R.id.imageView);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
String imageurl = "http://developer.android.com/assets/images/home/honeycomb-android.png";
ImageDownloadMessageHandler imageDownloadMessageHandler1= new ImageDownloadMessageHandler(progressBar, mainImageView);
ImageDownlaodThread imageDownlaodThread = new ImageDownlaodThread(imageDownloadMessageHandler1,imageurl);
imageDownlaodThread.start();
}
class ImageDownlaodThread extends Thread {
ImageDownloadMessageHandler imageDownloadMessageHandler;
String imageUrl;
public ImageDownlaodThread(ImageDownloadMessageHandler imageDownloadMessageHandler, String imageUrl) {
this.imageDownloadMessageHandler = imageDownloadMessageHandler;
this.imageUrl = imageUrl;
}
@Override
public void run() {
Drawable drawable = LoadImageFromWebOperations(imageUrl);
Message message = imageDownloadMessageHandler.obtainMessage(1, drawable);
imageDownloadMessageHandler.sendMessage(message);
System.out.println("Message sent");
}
}
class ImageDownloadMessageHandler extends Handler {
ProgressBar progressBar;
View imageTextView;
public ImageDownloadMessageHandler(ProgressBar progressBar, View imageTextView) {
this.progressBar = progressBar;
this.imageTextView = imageTextView;
}
@Override
public void handleMessage(Message message) {
progressBar.setVisibility(View.GONE);
imageTextView.setBackgroundDrawable(((Drawable) message.obj));
imageTextView.setVisibility(View.VISIBLE);
}
}
Drawable LoadImageFromWebOperations(String url) {
Drawable d = null;
InputStream is = null;
try {
is = (InputStream) new URL(url).getContent();
d = Drawable.createFromStream(is, "src name");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return d;
}
}
</code></pre><br />
<span style="background-color: #ead1dc;"></span><br />
<div dir="rtl" style="text-align: right;"><br />
</div></div>Anonymoushttp://www.blogger.com/profile/04135982125018256660noreply@blogger.com6