Exporting a SQLite database to an XML file in Android - java

I know this is possible but I'm not really sure where to start. Has anyone been able to achieve this?
Thanks.

The DataXmlExporter class described in this article will export a SQL lite DB to an XML file.
http://www.screaming-penguin.com/node/7749
The full example is available in this SVN repo. The ManageData class invokes the export.
http://totsp.com/svn/repo/AndroidExamples/trunk/
You will need to create an application class that exposes the DB and referenced as the application name in the AndroidManifest.xml file. Then use that DB as the argument to the DataXmlExporter constructor.
Here's the application class I use. You should already have a class (probably not named DatabaseHelper) that extends SQLiteOpenHelper
package com.billybobbain.android.someapp;
import android.app.Application;
import android.util.Log;
public class MyApplication extends Application {
public static final String APP_NAME = "SomeApp";
private DatabaseHelper dataHelper;
#Override
public void onCreate() {
super.onCreate();
Log.d(APP_NAME, "APPLICATION onCreate");
this.dataHelper = new DatabaseHelper(this);
}
#Override
public void onTerminate() {
Log.d(APP_NAME, "APPLICATION onTerminate");
super.onTerminate();
}
public DatabaseHelper getDataHelper() {
return this.dataHelper;
}
public void setDataHelper(DatabaseHelper dataHelper) {
this.dataHelper = dataHelper;
}
}

Have a look at the source code here exporting-a-sqlite-database-to-an-xml-file-in-android
The only change I had to make (to stop a few Eclipse warnings) was to close a cursor in the exportData( ) method. To make the code more portable, I also passed the XML file and location as an argument rather then as a declared final field.
The code writes the XML file to the SD card. Now, #mmaitlen who listed the source code on his blog doesn't add in any features to test for the existence of an external storage unit. So that's left for you to do.
However, you can embed some simple code to test for the existence of a writeable memory card with the following snippet (untested):
sdOkToWrite = false;
String sdTest = Environment.getExternalStorageState();
if (sdTest.equals(Environment.MEDIA_MOUNTED)) {
sdOkToWrite = true;
} else {
// Here's where you can code what to do without the external storage
}
Testing for the external storage is useful if you have large files to create that may exceed internal capacity.

I found this very helpful:
http://www.phonesdevelopers.com/1788273/
Using it the following way to export it to the sd card:
File sd = Environment.getExternalStorageDirectory();
String path = sd + "/" + DB_NAME + ".xml";
DatabaseDump databaseDump = new DatabaseDump(mySQLiteOpenHelperObject.getReadableDatabase(), path);
databaseDump.exportData();
Of course don't forget:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Related

ClassNotFoundException for stub while activating a remote object

public void Test {
String location = xyz; //location of the class file for the stub
ActivationDesc desc = new ActivationDesc( "TestObjectImpl", location, null);
ActivationID id = ActivationGroup.getSystem().registerObject(desc);
TestObject to = (TestObject)id.activate(true);
}
On running the above code, I get ClassNotFoundException for TestObjectImpl_stub . I looked around the web and found two possible workarounds:
Specify the path of the class files in CLASSPATH variable.
While executing the test, mention java.rmi.server.codebase=location along with the java command, location being the location of the class files.
Which of the above method is more appropriate? Is there a better solution for this?

How to create an empty folder in google cloud bucket using Java API

I am using the JSON API - Google API Client Library for Java to access the objects in Google Cloud Storage. I need to create (not upload) an empty folder in the bucket. Google Developer Web Console has that option to creating a directory, but neither the Java API nor the gsutil command has a create folder command. If anybody knows how to do so, please let me know. Thanks in advance...
You can emulate a folder by uploading a zero-sized object with a trailing slash.
As noted in the question comments, Google Cloud Storage is not a filesystem and emulating folders has serious limitations.
I think is better that you create the folder within the file name. For example if you need a folder called images and other one called docs, when you give the name of the object to upload do it in the following way images/name_of_file or docs/name_of_file.
If the name of the file is images/dogImage and you upload that file, you will find in your bucket a folder called images.
I hope to help you and others
This is my Java method to create an empty (emulated) folder:
public static void createFolder(String name) throws IOException {
Channels.newOutputStream(
createFile(name + "/")
).close();
}
public static GcsOutputChannel createFile(String name) throws IOException {
return getService().createOrReplace(
new GcsFilename(getName(), name),
GcsFileOptions.getDefaultInstance()
);
}
private static String name;
public static String getName() {
if (name == null) {
name = AppIdentityServiceFactory.getAppIdentityService().getDefaultGcsBucketName();
}
return name;
}
public static GcsService service;
public static GcsService getService() {
if (service == null) {
service = GcsServiceFactory.createGcsService(
new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
}
return service;
}

Liquibase execution order of changeset files when using includeAll with classpath*:

I am using liquibase (3.1.1) in a spring environment (3.2.x) and load the changesets via the inlcudeAll tag in a master file. There I use the "classpath*:/package/to/changesets" as path.
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<includeAll path="classpath*:/package/to/changesets"/>...
I use a naming strategy like "nnn_changesetname.xml" to keep ordering. But when I look into the changeset table this ordering via the filenames are not kept. Is this only working, if the changeset files are contained in a directory and not on the classpath?
Update
Hi, I found out that the below suggested solution is not enough. I think it lies in the implementation how liquibase resolves the includAll attribute. In my case it first resolves all "folders" and then looks into each folder for changeset xmls. This will break the ordering of the xml files in all classpath*:/changes locations, because there are now several "changes" folders in different locations. What I would suspect in such a case is a merge of all contents of this "virtual" classpath folders and loading of all resources in one enumeration. Or we could allow some resouce pattern in the inlcudeAll tag like resources="classpath*:/changes/*.xml" to directly select all needed files (tried it out with the path attribute, but did not work, because it checks for a folder)?
Update
I made a hack to check if the ordering in the returned enumeration is preserved with the anwser from below. To achive this I checked for the given package name and if it matches my pattern I added an additional "*.xml" to it. With this extension I get all changeset as needed.
#Override
public Enumeration<URL> getResources(String packageName)
throws IOException {
if(packageName.equals("classpath*:/plugin/liquibase/changes/")) {
packageName = packageName + "*.xml";
}
List<URL> resources = Collections.list(super.getResources(packageName));
Collections.sort(resources, new Comparator<URL>() {
#Override
public int compare(URL url1, URL url2) {
String path1 = FilenameUtils.getName(url1.getPath());
String path2 = FilenameUtils.getName(url2.getPath());
return String.CASE_INSENSITIVE_ORDER.compare(path1, path2);
}
});
logger.info("Found resources: {}", resources);
return Collections.enumeration(resources);
}};
In the log I can see now that the resources have the correct order. But when I look into the table DATABASECHANGELOCK it does not reflect the order I had in the enumeration. So it seems that this values get reodered somewhere else.
Update
Analyzed the code furhter and found out that the class liquibase.parser.core.xml.XMLChangeLogSAXHandler makes a reordering of the returned enumeration. So my changes will have no effect. I do not think that I can hack into this class as well.
You are right, Liquibase is relying on the underlying "list files" logic which orders files alphabetically through the file system but apparently does not through classpaths.
I created https://liquibase.jira.com/browse/CORE-1843 to track the fix.
For now, if you configure spring with a subclass of liquibase.integration.spring.SpringLiquibase that overrides getResources(String packageName) with a method that sorts the returned Enumeration that should resolve the problem for you.
So after some thinking and one night of sleep I came up with the following hack to guarantee order of the loaded changelog files via classpath pattern classpath*:/my/path/to/changelog/*.xml . The idea is to create the main changelog file on the fly via dom manipulation, when liquibase requests it.
It only works for the main changelog file. Following prerequisite:
The pattern can only be used for the main changelog file
I use an empty master changelog file as template
All other changelog files have to use the normal allowed loading mechanism
Works only in an Spring environment
First I had to extend/overwrite the liquibase.integration.spring.SpringLiquibase with my implementation.
public class MySpringLiquibase extends SpringLiquibase {
private static final Logger logger = LoggerFactory.getLogger(MySpringLiquibase.class);
private ApplicationContext context;
private String changeLogLocationPattern;
private List<String> changeLogLocations;
#Autowired
public void setContext(ApplicationContext context) {
this.context = context;
}
/**
* Location pattern to search for changelog files.
*
* #param changeLogLocationPattern
*/
public void setChangeLogLocationPattern(String changeLogLocationPattern) {
this.changeLogLocationPattern = changeLogLocationPattern;
}
#Override
public void afterPropertiesSet() throws LiquibaseException {
try {
changeLogLocations = new ArrayList<String>();
// retrieve all changelog resources for the pattern
List<Resource> changeLogResources = Arrays.asList(context.getResources(changeLogLocationPattern));
for (Resource changeLogResource : changeLogResources) {
// get only the classpath path of the resource
String changeLogLocation = changeLogResource.getURL().getPath();
changeLogLocation = "classpath:" + StringUtils.substringAfterLast(changeLogLocation, "!");
changeLogLocations.add(changeLogLocation);
}
// sort all found resources by string
Collections.sort(changeLogLocations, String.CASE_INSENSITIVE_ORDER);
} catch (IOException e) {
throw new LiquibaseException("Could not resolve changeLogLocationPattern", e);
}
super.afterPropertiesSet();
}
#Override
protected SpringResourceOpener createResourceOpener() {
final String mainChangeLog = getChangeLog();
return new SpringResourceOpener(getChangeLog()) {
#Override
public InputStream getResourceAsStream(String file)
throws IOException {
// check if main changelog file
if(mainChangeLog.equals(file)) {
// load master template and convert to dom object
Resource masterResource = getResourceLoader().getResource(file);
Document masterDocument = DomUtils.parse(masterResource, true);
// add all changelog locations as include elements
for (String changeLogLocation : changeLogLocations) {
Element inlcudeElement = masterDocument.createElement("include");
inlcudeElement.setAttribute("file", changeLogLocation);
masterDocument.getDocumentElement().appendChild(inlcudeElement);
}
if(logger.isDebugEnabled()) {
logger.debug("Master changeset: {}", DomUtils.toString(masterDocument));
}
// convert dom back to string and give it back as input resource
return new ByteArrayInputStream(DomUtils.toBytes(masterDocument));
} else {
return super.getResourceAsStream(file);
}
}
};
}
}
This class now needs to be used in the spring xml configuration.
<bean id="liquibase" class="liquibase.integration.spring.MySpringLiquibase"
p:changeLog="classpath:/plugin/liquibase/master.xml"
p:dataSource-ref="dataSource"
p:contexts="${liquibase.contexts:prod}"
p:ignoreClasspathPrefix="true"
p:changeLogLocationPattern="classpath*:/plugin/liquibase/changes/*.xml"/>
With this changes I have achieved that my main changelog files are ordered by their name.
Hope that helps others too.

java.lang.NullPointerException while running an application

I am new to android development. I downloaded source file from the following link
"http://android-er.blogspot.in/2012/07/implement-custom-linearlayout-for.html", but while trying to run in emulator it shows
java.lang.NullPointerException at com.example.androidhorizontalscrollviewgallery.MainActivity.onCreate(MainActivity.java:27)
My MainActivity.java code is below:
package com.example.androidhorizontalscrollviewgallery;
import java.io.File;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
MyHorizontalLayout myHorizontalLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHorizontalLayout = (MyHorizontalLayout)findViewById(R.id.mygallery);
File targetDir=getDir("Pictures",Context.MODE_PRIVATE);
String targetPath=targetDir+ "/homepage/";
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for(File f : files){
myHorizontalLayout.add(f.getAbsolutePath());
}
}
}
Here I have stored my images in my computer in "Libraries\Pictures\homepage" path. I wanted to get the images in gallery but I could not. I don know what causes this exception can anyone please tell the solution
try this..
File targetDir=getDir("Libraries\Pictures\homepage",Context.MODE_PRIVATE);
String targetPath=targetDir.toString();
From the exception, you have a null pointer on line 27, which means that the files object that you are iterating is null. You use the listFiles method to obtain that object, but the listFiles method can return null. You have to check whether you have null or not to avoid your app crashing. See example below. Also, refer to the File class documentation.
if (files == null) {
// handle case where the file object is not a directory
}
else {
for(File f : files){
myHorizontalLayout.add(f.getAbsolutePath());
}
}
I think you can't directly get images from your computer path.
You have to copy that images in your drawable or assests folder of the application.
Or your images should reside in either your device or emulator gallery or any other folders. But it should be in device or emulator in whichever you are testing.
See this line in tutorial you used.
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/test/";
So this is your device's sdcard path - external storage path.

Mount an external folder as a resource in Wicket

What I am trying to accomplish is for some image references in a css file to be located in a folder seperate to the actual application.
Is it possible to mount an external folder as a resource in Wicket?
In pseudocode this is what I am trying to do:
public class Application extends WicketApplication
{
init()
{
mountResource(new FolderResource("Path/to/some/folder", "someid"));
}
}
So that the .css class would reference the resources like this:
.someclass
{
url("resources/someid/images/image.png")
}
I'm sure I've seen this somewhere but I just can't seem to be able to find it again...
EDIT
Should also note that im currently running on Wicket 1.4
As simple as following.
MyApplication.java:
public class MyApplication extends WebApplication {
...
public void init() {
...
final String resourceId = "images";
getSharedResources().add(resourceId, new FolderResource(new File(getServletContext().getRealPath("img"))));
mountSharedResource("/image", Application.class.getName() + "/" + resourceId);
}
...
}
FolderResource.java:
public class FolderResource extends WebResource {
private File folder;
public FolderResource(File folder) {
this.folder = folder;
}
#Override
public IResourceStream getResourceStream() {
String fileName = getParameters().getString("file");
File file = new File(folder, fileName);
return new FileResourceStream(file);
}
}
And then you can get any image from "img" folder inside your application by simple URL:
/your-application/app/image?file=any-image.png
Here "/your-application" is the application context path, "/app" is the Wicket servlet mapping in web.xml, and "any-image.png" is the name of the image file.

Categories

Resources