Search in sources :

Example 61 with SQLiteException

use of android.database.sqlite.SQLiteException in project qksms by moezbhatti.

the class PduPersister method convertUriToPath.

/**
 * This method expects uri in the following format
 *     content://media/<table_name>/<row_index> (or)
 *     file://sdcard/test.mp4
 *     http://test.com/test.mp4
 *
 * Here <table_name> shall be "video" or "audio" or "images"
 * <row_index> the index of the content in given table
 */
public static String convertUriToPath(Context context, Uri uri) {
    String path = null;
    if (null != uri) {
        String scheme = uri.getScheme();
        if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) {
            path = uri.getPath();
        } else if (scheme.equals("http")) {
            path = uri.toString();
        } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
            String[] projection = new String[] { MediaStore.MediaColumns.DATA };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
                    throw new IllegalArgumentException("Given Uri could not be found" + " in media store");
                }
                int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                path = cursor.getString(pathIndex);
            } catch (SQLiteException e) {
                throw new IllegalArgumentException("Given Uri is not formatted in a way " + "so that it can be found in media store.");
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        } else {
            throw new IllegalArgumentException("Given Uri scheme is not supported");
        }
    }
    return path;
}
Also used : Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Example 62 with SQLiteException

use of android.database.sqlite.SQLiteException in project qksms by moezbhatti.

the class UriImage method getOrientation.

/**
 * Returns the number of degrees to rotate the picture, based on the orientation tag in
 * the exif data or the orientation column in the database. If there's no tag or column,
 * 0 degrees is returned.
 *
 * @param context Used to get the ContentResolver
 * @param uri Path to the image
 */
public static int getOrientation(Context context, Uri uri) {
    long dur = System.currentTimeMillis();
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme()) || sURLMatcher.match(uri) == MMS_PART_ID) {
        // file for the orientation because there is no column in the db for the orientation.
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            ExifInterface exif = new ExifInterface();
            try {
                exif.readExif(inputStream);
                Integer val = exif.getTagIntValue(ExifInterface.TAG_ORIENTATION);
                if (val == null) {
                    return 0;
                }
                int orientation = ExifInterface.getRotationForOrientationValue(val.shortValue());
                return orientation;
            } catch (IOException e) {
                Log.w(TAG, "Failed to read EXIF orientation", e);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Can't open uri: " + uri, e);
        } finally {
            dur = System.currentTimeMillis() - dur;
            if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
                Log.v(TAG, "UriImage.getOrientation (exif path) took: " + dur + " ms");
            }
        }
    } else {
        // Try to get the orientation from the ORIENTATION column in the database. This is much
        // faster than reading all the exif tags from the file.
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, new String[] { Images.ImageColumns.ORIENTATION }, null, null, null);
            if (cursor.moveToNext()) {
                int ori = cursor.getInt(0);
                return ori;
            }
        } catch (SQLiteException e) {
        } catch (IllegalArgumentException e) {
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            dur = System.currentTimeMillis() - dur;
            if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
                Log.v(TAG, "UriImage.getOrientation (db column path) took: " + dur + " ms");
            }
        }
    }
    return 0;
}
Also used : InputStream(java.io.InputStream) ExifInterface(com.moez.QKSMS.exif.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Example 63 with SQLiteException

use of android.database.sqlite.SQLiteException in project android-sqlite-asset-helper by jgilfelt.

the class SQLiteAssetHelper method getWritableDatabase.

/**
 * Create and/or open a database that will be used for reading and writing.
 * The first time this is called, the database will be extracted and copied
 * from the application's assets folder.
 *
 * <p>Once opened successfully, the database is cached, so you can
 * call this method every time you need to write to the database.
 * (Make sure to call {@link #close} when you no longer need the database.)
 * Errors such as bad permissions or a full disk may cause this method
 * to fail, but future attempts may succeed if the problem is fixed.</p>
 *
 * <p class="caution">Database upgrade may take a long time, you
 * should not call this method from the application main thread, including
 * from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
 *
 * @throws SQLiteException if the database cannot be opened for writing
 * @return a read/write database object valid until {@link #close} is called
 */
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
        // The database is already open for business
        return mDatabase;
    }
    if (mIsInitializing) {
        throw new IllegalStateException("getWritableDatabase called recursively");
    }
    // If we have a read-only database open, someone could be using it
    // (though they shouldn't), which would cause a lock to be held on
    // the file, and our attempts to open the database read-write would
    // fail waiting for the file lock.  To prevent that, we acquire the
    // lock on the read-only database, which shuts out other users.
    boolean success = false;
    SQLiteDatabase db = null;
    // if (mDatabase != null) mDatabase.lock();
    try {
        mIsInitializing = true;
        // if (mName == null) {
        // db = SQLiteDatabase.create(null);
        // } else {
        // db = mContext.openOrCreateDatabase(mName, 0, mFactory);
        // }
        db = createOrOpenDatabase(false);
        int version = db.getVersion();
        // do force upgrade
        if (version != 0 && version < mForcedUpgradeVersion) {
            db = createOrOpenDatabase(true);
            db.setVersion(mNewVersion);
            version = db.getVersion();
        }
        if (version != mNewVersion) {
            db.beginTransaction();
            try {
                if (version == 0) {
                    onCreate(db);
                } else {
                    if (version > mNewVersion) {
                        Log.w(TAG, "Can't downgrade read-only database from version " + version + " to " + mNewVersion + ": " + db.getPath());
                    }
                    onUpgrade(db, version, mNewVersion);
                }
                db.setVersion(mNewVersion);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }
        onOpen(db);
        success = true;
        return db;
    } finally {
        mIsInitializing = false;
        if (success) {
            if (mDatabase != null) {
                try {
                    mDatabase.close();
                } catch (Exception e) {
                }
            // mDatabase.unlock();
            }
            mDatabase = db;
        } else {
            // if (mDatabase != null) mDatabase.unlock();
            if (db != null)
                db.close();
        }
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) IOException(java.io.IOException) SQLiteException(android.database.sqlite.SQLiteException)

Example 64 with SQLiteException

use of android.database.sqlite.SQLiteException in project android-sqlite-asset-helper by jgilfelt.

the class SQLiteAssetHelper method getReadableDatabase.

/**
 * Create and/or open a database.  This will be the same object returned by
 * {@link #getWritableDatabase} unless some problem, such as a full disk,
 * requires the database to be opened read-only.  In that case, a read-only
 * database object will be returned.  If the problem is fixed, a future call
 * to {@link #getWritableDatabase} may succeed, in which case the read-only
 * database object will be closed and the read/write object will be returned
 * in the future.
 *
 * <p class="caution">Like {@link #getWritableDatabase}, this method may
 * take a long time to return, so you should not call it from the
 * application main thread, including from
 * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
 *
 * @throws SQLiteException if the database cannot be opened
 * @return a database object valid until {@link #getWritableDatabase}
 *     or {@link #close} is called.
 */
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
    if (mDatabase != null && mDatabase.isOpen()) {
        // The database is already open for business
        return mDatabase;
    }
    if (mIsInitializing) {
        throw new IllegalStateException("getReadableDatabase called recursively");
    }
    try {
        return getWritableDatabase();
    } catch (SQLiteException e) {
        // Can't open a temp database read-only!
        if (mName == null)
            throw e;
        Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
    }
    SQLiteDatabase db = null;
    try {
        mIsInitializing = true;
        String path = mContext.getDatabasePath(mName).getPath();
        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
        if (db.getVersion() != mNewVersion) {
            throw new SQLiteException("Can't upgrade read-only database from version " + db.getVersion() + " to " + mNewVersion + ": " + path);
        }
        onOpen(db);
        Log.w(TAG, "Opened " + mName + " in read-only mode");
        mDatabase = db;
        return mDatabase;
    } finally {
        mIsInitializing = false;
        if (db != null && db != mDatabase)
            db.close();
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteException(android.database.sqlite.SQLiteException)

Example 65 with SQLiteException

use of android.database.sqlite.SQLiteException in project DevRing by LJYcoder.

the class NativeTableManager method loadOne.

@Override
public M loadOne(K key) {
    M m = null;
    String sql = "select * from " + mTableName + "where " + getPkName() + "=?";
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(sql, new String[] { String.valueOf(key) });
        m = cursor != null ? readCursor(cursor) : null;
    } catch (SQLiteException e) {
        RingLog.e(e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
        mOpenHelper.close();
    }
    return m;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException)

Aggregations

SQLiteException (android.database.sqlite.SQLiteException)122 Cursor (android.database.Cursor)72 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)36 ContentValues (android.content.ContentValues)28 SQLException (android.database.SQLException)17 Intent (android.content.Intent)14 HandlerThread (android.os.HandlerThread)10 File (java.io.File)10 HashMap (java.util.HashMap)8 Account (android.accounts.Account)7 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)7 SyncStatusInfo (android.content.SyncStatusInfo)6 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)6 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 Uri (android.net.Uri)5 ArrayList (java.util.ArrayList)5 SuppressLint (android.annotation.SuppressLint)4 SQLiteStatement (android.database.sqlite.SQLiteStatement)3 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)3