Android SQLite:将照片作为Base64字符串保存到DB。。。然后取回
我想为新的安卓开发者开发一个针对常见问题的全面解决方案。我想创建一个教程和示例(供我和其他人学习),在应用程序中拍照,然后将其保存到SQLite android数据库中的base64字符串。然后,我将能够在稍后阶段将此字符串上传到在线数据库,而不是我最初拍摄的JPG图像 所以要重新迭代它必须Android SQLite:将照片作为Base64字符串保存到DB。。。然后取回,android,database,image,sqlite,base64,Android,Database,Image,Sqlite,Base64,我想为新的安卓开发者开发一个针对常见问题的全面解决方案。我想创建一个教程和示例(供我和其他人学习),在应用程序中拍照,然后将其保存到SQLite android数据库中的base64字符串。然后,我将能够在稍后阶段将此字符串上传到在线数据库,而不是我最初拍摄的JPG图像 所以要重新迭代它必须 打开摄像机 拍照 另存为Base64字符串到SQLite DB (我需要将其作为字符串存储在DB中的原因是,这将在将来某个时候上载到中央文档系统,而大型JPG图像将很慢且不理想,因此为什么DB中的Bas
- 打开摄像机
- 拍照
- 另存为Base64字符串到SQLite DB
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inspection ID" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtName"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1" />
<EditText
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:gravity="top|left"
android:lines="4"
android:maxLines="4"
android:minLines="4"
android:scrollbars="vertical"
android:singleLine="false"
android:width="0dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Ref"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtAge"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drop Down"
/>
<Spinner
android:id="@+id/spinDept"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnPhotoCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="camera" />
<Button
android:id="@+id/btnPhotoGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="gallery" />
<TextView
android:id="@+id/lblDisplayImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCancel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="below_this_text_image_will_be_displayed"
android:textSize="13dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/lblDisplayImage"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:gravity="bottom" >
<!--
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
-->
<ImageView
android:id="@+id/imgDisplayImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="area_where_image_is_to_be_displayed" />
<!-- </ScrollView> -->
</RelativeLayout>
<Button
android:id="@+id/btnAudio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnAudio"
android:text="Audio" />
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnAddEmp_Click"
android:text="Save Inspection" />
<Button
android:id="@+id/btnCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/btnPhotoGallery"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Reset/Clear Form Data" />
<TextView
android:id="@+id/txtEmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of Inspections on Device " />
</LinearLayout>
</ScrollView>
</LinearLayout>
>
使用下面的.java
package mina.android.DatabaseDemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Spannable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import com.AssentApp.V100.R;
public class AddEmployee extends Activity {
EditText txtName;
EditText txtAge;
TextView txtEmps;
DatabaseHelper dbHelper;
Spinner spinDept;
/** The Constant PICK_IMAGE. */
private static final int PICK_IMAGE = 0;
/** The Constant PICK_IMAGE_FROM_GALLERY. */
private static final int PICK_IMAGE_FROM_GALLERY = 1;
/** The btn cancel. */
private Button btnPhotoCamera,btnPhotoGallery,btnCancel;
/** The img view. */
private ImageView imgView;
/** The u. */
private Uri u;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addemployee);
txtName=(EditText)findViewById(R.id.txtName);
txtAge=(EditText)findViewById(R.id.txtAge);
txtEmps=(TextView)findViewById(R.id.txtEmps);
spinDept=(Spinner)findViewById(R.id.spinDept);
imgView=(ImageView)findViewById(R.id.imgDisplayImage);
btnPhotoCamera=(Button)findViewById(R.id.btnPhotoCamera);
btnPhotoGallery=(Button)findViewById(R.id.btnPhotoGallery);
btnCancel=(Button)findViewById(R.id.btnCancel);
btnPhotoCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent camera=new Intent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra("crop", "false");
File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
u = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myFile.jpg"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, u);
startActivityForResult(camera, PICK_IMAGE);
}
});
btnPhotoGallery.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_FROM_GALLERY);
}
});
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent goStartUp=new Intent(AddEmployee.this, AddEmployee.class);
goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goStartUp);
finish();
}
});
}
/* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode==RESULT_OK )
{
if(requestCode == PICK_IMAGE) {
InputStream is=null;
try {
is = this.getContentResolver().openInputStream(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp=BitmapFactory.decodeStream(is);
imgView.setImageBitmap(bmp);
Log.i("Inside", "PICK_IMAGE");
}
if (requestCode == PICK_IMAGE_FROM_GALLERY) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Log.d("data",filePathColumn[0]);
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imgView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Log.i("Inside", "PICK_IMAGE_FROM_GALLERY");
}
}
}
public void onStart()
{
try
{
super.onStart();
dbHelper=new DatabaseHelper(this);
txtEmps.setText(txtEmps.getText()+String.valueOf(dbHelper.getEmployeeCount()));
Cursor c=dbHelper.getAllDepts();
startManagingCursor(c);
//SimpleCursorAdapter ca=new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, c, new String [] {DatabaseHelper.colDeptName}, new int []{android.R.id.text1});
SimpleCursorAdapter ca=new SimpleCursorAdapter(this,R.layout.deptspinnerrow, c, new String [] {DatabaseHelper.colDeptName,"_id"}, new int []{R.id.txtDeptName});
//ca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinDept.setAdapter(ca);
spinDept.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View selectedView,
int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//never close cursor
}
catch(Exception ex)
{
CatchError(ex.toString());
}
}
public void btnAddEmp_Click(View view)
{
boolean ok=true;
try
{
Spannable spn=txtAge.getText();
String name=txtName.getText().toString();
int age=Integer.valueOf(spn.toString());
int deptID=Integer.valueOf((int)spinDept.getSelectedItemId());
Employee emp=new Employee(name,age,deptID);
dbHelper.AddEmployee(emp);
}
catch(Exception ex)
{
ok=false;
CatchError(ex.toString());
}
finally
{
if(ok)
{
//NotifyEmpAdded();
Alerts.ShowEmpAddedAlert(this);
txtEmps.setText("Number of Inspections on Device "+String.valueOf(dbHelper.getEmployeeCount()));
}
}
}
void CatchError(String Exception)
{
Dialog diag=new Dialog(this);
diag.setTitle("Adding new Inspection");
TextView txt=new TextView(this);
txt.setText(Exception);
diag.setContentView(txt);
diag.show();
}
void NotifyEmpAdded()
{
Dialog diag=new Dialog(this);
diag.setTitle("Success");
TextView txt=new TextView(this);
txt.setText("Inspection Added Successfully");
diag.setContentView(txt);
diag.show();
try {
diag.wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
CatchError(e.toString());
}
diag.notify();
diag.dismiss();
}
public void btnAudio(View view)
{
Intent intent = new Intent(AddEmployee.this, AudioRecordTest.class);
startActivity(intent);
}
}
包mina.android.DatabaseDemo;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.InputStream;
导入android.app.Activity;
导入android.app.Dialog;
导入android.content.Intent;
导入android.database.Cursor;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Environment;
导入android.provider.MediaStore;
导入android.text.Spannable;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.AdapterView;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.SimpleCursorAdapter;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.widget.AdapterView.OnItemSelectedListener;
导入com.AssentApp.V100.R;
公共类AddEmployee扩展活动{
编辑文本txtName;
编辑文本文本;
文本视图txtEmps;
数据库助手dbHelper;
纺纱机;
/**常量PICK_图像*/
私有静态最终整型拾取图像=0;
/**从库中不断拾取图像*/
私有静态最终整型从图片库中选取图片=1;
/**btn取消*/
私人按钮btnPhotoCamera、btnPhotoGallery、btnCancel;
/**img视图*/
私有图像视图imgView;
/**美国*/
私有Uri u;
/*(非Javadoc)
*@see android.app.Activity#onCreate(android.os.Bundle)
*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.addemployee);
txtName=(EditText)findViewById(R.id.txtName);
txtAge=(EditText)findViewById(R.id.txtAge);
txtEmps=(TextView)findViewById(R.id.txtEmps);
spinDept=(微调器)findViewById(R.id.spinDept);
imgView=(ImageView)findViewById(R.id.imgDisplayImage);
btnPhotoCamera=(按钮)findviewbyd(R.id.btnPhotoCamera);
btnPhotoGallery=(按钮)findViewById(R.id.btnPhotoGallery);
btnCancel=(按钮)findViewById(R.id.btnCancel);
setOnClickListener(新的OnClickListener()){
公共void onClick(视图v){
意图照相机=新意图();
摄像头.setAction(MediaStore.ACTION\u图像捕获);
相机。额外(“裁剪”、“假”);
文件f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u图片);
u=Uri.fromFile(新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_图片),“myFile.jpg”);
camera.putExtra(MediaStore.EXTRA_输出,u);
startActivityForResult(摄像头、拾取图像);
}
});
btnPhotoGallery.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
意图=新的意图(Intent.ACTION_