Recent Notes
Displaying keyword search results 1 - 10
Created by lokf on January 13, 2012 14:10:42
Last update: January 13, 2012 14:10:42
For some reason I don't know writing to files in Android is very complicated and tedious. Here is some code for those who might need it. Apps should write in the SD in the directory /Android/data/package_name/files/ so that it is deleted with the app uninstall. package randomname; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.ByteBuffer; import android.content.Context; import android.os.Environment; import android.util.Log; public class FileIOLibrary { String packageName; boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; FileIOLibrary(String packageNamep) { this.packageName=packageNamep; } public boolean isExternalStorageAvailable() { updateExternalStorageState(); return mExternalStorageAvailable; } public boolean isExternalStorageWritable() { updateExternalStorageState(); return mExternalStorageWriteable; } /** * writes the current state of SD card to the corresponding variables */ void updateExternalStorageState() {...
Created by Dr. Xi on November 11, 2011 10:05:22
Last update: November 11, 2011 10:12:01
This is an HTML image tag filter using Java regex. It takes a string, finds the img tags, replaces the src attribute with one provided by the filter, then adds a class name to the class attribute.
import java.util.regex.*;
import java.io.*;
...
Test file:
<div id="HTML snippet">
<img src="img/big/txt-m...
Created by alfa on July 15, 2011 13:25:45
Last update: July 15, 2011 13:25:45
Read the whole contents of a file into a String. It's better to read the whole file as bytes and convert to String than to read the file line by line and concatenate the lines.
String getFileContents(String fileName) throws...
Using java.nio :
import java.io.FileInputStream;
import java...
Created by Dr. Xi on July 13, 2011 16:18:05
Last update: July 13, 2011 16:18:05
The goal is to read a file like this:
for (String line: textFileReader) {
// do s...
This is the code:
import java.io.*;
import java.util.Iterator;
...
Created by Dr. Xi on June 21, 2011 15:54:00
Last update: June 22, 2011 11:33:09
Demo code for CSV parsing with SuperCSV parser .
Java code:
import java.io.*;
import java.util.List;
imp...
Test with a simple CSV file:
psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,...
The parser worked correctly:
Line 1 has 11 values:
|psmith01|
|CLASS2B|...
Test with a more complicated CSV file:
"psmith01 abc", "CLASS2B " , " Peter...
The parser messed up on all three lines:
Line 1 has 5 values:
|psmith01 abc|
|CLASS...
Using two lines input:
"psmith01 abc", "CLASS2B " , " Peter...
It generates an exception:
Line 1 has 5 values:
|psmith01 abc|
|CLASS...
Add a new line in item two:
"One", "Two
", "Three"
Result:
Line 2 has 3 values:
|One|
|Two
|
|...
Created by freyo on May 24, 2011 09:15:14
Last update: May 24, 2011 09:15:14
Java built-in X.509 certificate factory reads CertPath objects encoded in PkiPath or PKCS7 formats. The META-INF/<key_alias>.RSA file generated by Java jarsigner is in PKCS7 format.
Example code:
import java.util.*;
import java.io.*;
import...
PKCS7 files can also be generated by openssl from certificate file(s):
openssl crl2pkcs7 -nocrl -certfile Certs.pem -out ...
Convert PKCS7 from DER to PEM:
openssl pkcs7 -in certs.pk7 -inform DER -out certs...
Sample PKCS7 file:
-----BEGIN PKCS7-----
MIIERwYJKoZIhvcNAQcCoIIEO...
Created by Dr. Xi on May 17, 2011 21:14:58
Last update: May 17, 2011 21:17:58
Read certificate one at a time from BufferedInputStream :
// import java.security.cert.*;
public void rea...
Read all certificates from a file as a collection:
public void readCertificates(File f) throws Except...
Created by freyo on May 17, 2011 11:13:17
Last update: May 17, 2011 11:13:17
This is an odd-ball content provider in that it doesn't provide database records, but provides a resource as a stream. It can be used to provide media files or XML resources. Start the project with:
tools/android create project --package com.android... Create assets directory and add an XML file ( assets/demo.xml ): <? xml version="1.0" encoding="UTF-8"?> <people... Edit the layout ( res/layout/main.xml ): <?xml version="1.0" encoding="utf-8"?> <LinearL... Edit src/com/android/cptest/Dummy.java : package com.android.cptest; import java.io.... Add content provider ( src/com/android/cptest/XmlResource.java ): package com.android.cptest; import java.io.... Update AndroidManifest.xml : <?xml version="1.0" encoding="utf-8"?> <manifes... Add this section to the end of build.xml : <target name="-package-resources"> <ech... Build and install: ant install Screenshot: Remove the Dummy activity ( AndroidManifest.xml ): <?xml version="1.0" encoding="utf-8"?> <manifes... Create a new project for...
Created by freyo on May 09, 2011 15:13:59
Last update: May 09, 2011 15:14:22
This code snippet opens and reads the Android APK archive file for a given package name.
// import java.io.*;
// import java.util.zip.*;...
Created by freyo on May 09, 2011 14:15:01
Last update: May 09, 2011 14:17:22
In short, use the PackageManager class to get the PackageInfo :
PackageInfo pkgInfo = getPackageManager().getP...
To build an APK, make these changes to the greetings app :
Change the layout to ( res/layout/main.xml ):
<?xml version="1.0" encoding="utf-8"?>
<LinearL...
Change the string values ( res/values/strings.xml ):
<?xml version="1.0" encoding="utf-8"?>
<resourc...
Update AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifes...
Create a new Java class src/com/android/appinfo/GetAppVersion.java :
package com.android.appinfo;
import android...
Edit build.xml , change project name to "AppInfo":
<?xml version="1.0" encoding="UTF-8"?>
<project...
Install to emulator:
ant install
Screenshot: