如何在单击按钮事件按钮时在sqlite数据库中存储图像?如何在单击按钮事件按钮时将图像存储在sqlite数据库中?
1
A
回答
9
package com.examples.aMySQL2;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
//Reading lines from a file
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
public class aMySQL2Activity extends Activity implements OnClickListener
{
protected static TextView textView;
protected static ImageView bmImage;
protected Button start;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bmImage = (ImageView)findViewById(R.id.imageView1);
textView = (TextView) findViewById(R.id.textView1);
start = (Button) findViewById(R.id.button1);
start.setOnClickListener(this);
DownloadFile();
}
// /data/data/<package_name>/databases -путь к базе данных по умолчанию
// "/data/data/com.examples.aMySQL2/databases/MyDB.db" //-путь к базе данных по умолчанию
// "/sdcard/Nick/MyDB.db"
// "/mnt/sdcard/Nick/MyDB.db"
/// Android/data/<package_name>/files/
public void onClick(View v)
{
SQLiteDatabase myDb;
String MySQL;
int icount;
byte[] byteImage1 = null;
byte[] byteImage2 = null;
MySQL="create table emp1(_id INTEGER primary key autoincrement, fio TEXT not null, picture BLOB);";
myDb = openOrCreateDatabase("/sdcard/Nick/MyWeatherDB.db", Context.MODE_PRIVATE, null);
// myDb.execSQL(MySQL);
String s=myDb.getPath();
textView.append("\r\n" + s+"\r\n");
myDb.execSQL("delete from emp1");
ContentValues newValues = new ContentValues();
newValues.put("fio", "Иванов Петр Сергеевич");
/////////// insert picture to blob field /////////////////////
try
{
FileInputStream instream = new FileInputStream("/sdcard/Nick/weather.png");
BufferedInputStream bif = new BufferedInputStream(instream);
byteImage1 = new byte[bif.available()];
bif.read(byteImage1);
textView.append("\r\n" + byteImage1.length+"\r\n");
newValues.put("picture", byteImage1);
long ret = myDb.insert("emp1", null, newValues);
if(ret<0) textView.append("\r\n!!! Error add blob filed!!!\r\n");
} catch (IOException e)
{
textView.append("\r\n!!! Error: " + e+"!!!\r\n");
}
////////////Read data ////////////////////////////
Cursor cur = myDb.query("emp1",null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false)
{
textView.append("\r\n" + cur.getString(1)+"\r\n");
cur.moveToNext();
}
///////Read data from blob field////////////////////
cur.moveToFirst();
byteImage2=cur.getBlob(cur.getColumnIndex("picture")); // ИМЕННО ТАК!!!
bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length));
textView.append("\r\n" + byteImage2.length+"\r\n");
//////////////////////////
cur.close();
myDb.close();
}
public void DownloadFile()
{
Bitmap bitmap1 = null;
bitmap1 = BitmapFactory.decodeFile("/sdcard/Nick/saranka.jpg"); //weather.png");
bmImage.setImageBitmap(bitmap1);
}
}
+0
谢谢你的工作。 – 2011-05-18 07:49:27
+0
我如何下载超过10张图片 – user915267 2012-08-09 09:54:29
1
你必须存储图像的完整url在按钮上点击数据库。 此外,只需使用该URL通过从数据库获取url来绘制图像。
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// insert url of the image into the database here
}
}
希望这会给你一些关于这个问题的想法。
相关问题
- 1. 如何将数据存储在单选按钮的数据库
- 2. 如何将按钮的值存储在点击事件jQuery中
- 3. 如何在数据库中存储单选按钮值?
- 4. 在按钮单击时将值存储在sharedpreference中
- 5. 单击按钮时插入数据库
- 6. 如何禁用之外按钮事件时,请单击单击按钮javacript
- 7. 使图像在按钮单击事件中可见
- 8. 如何将背景图像设置为快速按钮(单击按钮时)
- 9. UIWebView按钮单击事件
- 10. WPF按钮单击事件
- 11. 单击事件按钮
- 12. jquery按钮单击事件
- 13. 如何在android studio中单击按钮时绘制图像?
- 14. 如何在iPhone中单击时更改按钮背景图像?
- 15. 点击按钮事件没有在android中射击,同时保存图像
- 16. 如何在单击按钮时在按钮上打印值?
- 17. 增加按钮点击事件的图像按钮的亮度
- 18. 在CSS中,如何在按钮聚焦时增长按钮+按钮图像?
- 19. 按钮单击事件添加到数据库中,从
- 20. 检查单击的单选按钮是否存储在数据库中
- 21. sqlite插入按钮单击
- 22. jQuery的:在按钮的单击事件
- 23. 在按钮上单击显示图像
- 24. 按钮单击事件以在2之间传输图像Picturebox
- 25. 单击新按钮时杀死一个按钮事件
- 26. 在按钮单击事件中使用图像控件显示图像
- 27. 单击按钮时出现图像
- 28. 单击按钮时旋转图像
- 29. 如何在单击按钮上存储函数的返回值?
- 30. 如何通过单击该按钮将按钮转换为保存按钮
质量非常低的问题 – 2011-05-18 07:26:08
不仅是一个可怕的问题,它也是一个重复:http://stackoverflow.com/questions/5522866/using-images-and-videos-in-android-applicationsqlite-blob – EboMike 2011-05-18 07:29:56