Java tutorial
//package com.java2s; /* * Copyright (C) 2012 The Android Open Source Project * Copyright (C) 2009-2013 University of Washington * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ import java.io.File; import android.os.Environment; import android.util.Log; public class Main { private final static String t = "FileUtils"; private static final String ODK_FOLDER_NAME = "opendatakit"; public static String toAppPath(String fullpath) { String path = getOdkFolder() + File.separator; if (fullpath.startsWith(path)) { String partialPath = fullpath.substring(path.length()); String[] app = partialPath.split(File.separator); if (app == null || app.length < 1) { Log.w(t, "Missing file path (nothing under '" + ODK_FOLDER_NAME + "'): " + fullpath); return null; } return partialPath; } else { String[] parts = fullpath.split(File.separator); int i = 0; while (parts.length > i && !parts[i].equals(ODK_FOLDER_NAME)) { ++i; } if (i == parts.length) { Log.w(t, "File path is not under expected '" + ODK_FOLDER_NAME + "' Folder (" + path + ") conversion failed for: " + fullpath); return null; } int len = 0; // trailing slash while (i >= 0) { len += parts[i].length() + 1; --i; } String partialPath = fullpath.substring(len); String[] app = partialPath.split(File.separator); if (app == null || app.length < 1) { Log.w(t, "File path is not under expected '" + ODK_FOLDER_NAME + "' Folder (" + path + ") missing file path (nothing under '" + ODK_FOLDER_NAME + "'): " + fullpath); return null; } Log.w(t, "File path is not under expected '" + ODK_FOLDER_NAME + "' Folder -- remapped " + fullpath + " as: " + path + partialPath); return partialPath; } } public static String getOdkFolder() { String path = Environment.getExternalStorageDirectory() + File.separator + ODK_FOLDER_NAME; return path; } }