我正在开发一个Android应用程序,在其中我从URL获取单个图像,然后将其存储到Sq lite数据库中。
目前同样的图像(Abc.png)存储在sq lite数据库中的次数超过一次,当我一次又一次地启动Activity时......我希望那个相同的图像(Abc.png)不能存储多次。< / p>
如果在此之后数据库中没有行,则如何通过将其存储在数据库中来停止相同的图像(Abc.png)。
我怎么能这样做??
这是我的代码: -
PanoramaViewActivity
public class PanoramaViewActivity extends Activity {
// Set your Image URL into a string
String URL = "http://mw2.google.com/mw-panoramio/photos/medium/17287086.jpg";
ImageView image, imagetop;
ProgressDialog mProgressDialog;
private ImageDbAdapter mDbAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.panoramaview);
// Locate the ImageView in activity_main.xml
image = (ImageView) findViewById(R.id.img);
imagetop = (ImageView) findViewById(R.id.imgtop);
// Execute DownloadImage AsyncTask
new DownloadImage().execute(URL);
}
// DownloadImage AsyncTask
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(PanoramaViewActivity.this);
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// Set the bitmap into ImageView
image.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
insertToDB(result);
queryFromDB();
}
}
private void insertToDB(Bitmap result1) {
mDbAdapter = new ImageDbAdapter(this);
mDbAdapter.open();
Bitmap photo = result1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();
mDbAdapter.insert(bArray);
}
private void queryFromDB() {
byte[] image = mDbAdapter.fetchSingle(1);
imagetop.setImageBitmap(Utilities.getImage(image));
Log.d("result", "" + mDbAdapter.fetchSingle(1));
mDbAdapter.close();
}
}
公用事业类: -
public class Utilities {
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
ImageDbHelper
public class ImageDbHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "image_storage.db";
public static final int DB_VERSION = 1;
private static final String SQL_CREATE = "CREATE TABLE tbl_image ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "image_data BLOB"
+ ");";
private static final String SQL_DROP = "DROP TABLE IF EXISTS tbl_image;";
public ImageDbHelper(Context c) {
super(c, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DROP);
onCreate(db);
}
}
ImageDbAdapter
public class ImageDbAdapter {
public static final String TABLE_NAME = "tbl_image";
public static final String COL_ID = "_id";
public static final String COL_DATA = "image_data";
public static final String[] PROJECTION_ALL = new String[] { COL_ID,
COL_DATA };
private Context mContext;
private SQLiteDatabase mDb;
private ImageDbHelper mDbHelper;
public ImageDbAdapter(Context c) {
mContext = c;
}
public ImageDbAdapter open() throws SQLException {
mDbHelper = new ImageDbHelper(mContext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long insert(byte[] image) {
return mDb.insert(TABLE_NAME, null, createContentValues(image));
}
private ContentValues createContentValues(byte[] image) {
ContentValues cv = new ContentValues();
cv.put(COL_DATA, image);
return cv;
}
public byte[] fetchSingle(int id) {
Cursor queryCursor = mDb.query(TABLE_NAME, PROJECTION_ALL, COL_ID
+ " = " + id, null, null, null, null);
if (queryCursor == null) {
return null;
}
byte[] image = null;
if (queryCursor.moveToFirst()) {
image = queryCursor.getBlob(queryCursor
.getColumnIndexOrThrow(COL_DATA));
}
queryCursor.close();
return image;
}
}
答案 0 :(得分:0)
String URL ="mw2.google.com/mw-panoramio/photos/medium/17287086.jpg"
String[] bits = URL .split("/");
String imagename = bits[bits.length - 1];
System.out.println(imagename);
Result will be 17287086.jpg