0

新しい Android 開発者によくある問題に対する完全な解決策を開発したいと思います。アプリで写真を撮り、SQLite android データベース内の base64 文字列に保存するためのチュートリアルと例 (自分自身と他の人が従うため) を作成したいと考えています。その後、最初に撮影した JPG 画像ではなく、この文字列を後の段階でオンライン データベースにアップロードできます。

繰り返しますが、それは...

  • オープンカメラ、
  • 画像を撮る
  • Base64 文字列として SQLite DB に保存

(文字列として DB に格納する必要がある理由は、これが将来のある時点で中央ドキュメント システムにアップロードされるという事実によるものであり、大きな JPG 画像は遅く、理想的ではありません。したがって、DB の Base64 文字列の方が優れている理由 また、DB で参照される画像をユーザーが削除できる可能性を制限したいと思います。あまり適していないと思います))

ありがとう、私はこれに取り組み、他の人が従うことができる素晴らしいソリューションを開発することを本当に楽しみにしています.

これまでの私の努力...

私はこれを調査するのに何日も費やしましたが、現時点では最初よりも混乱しています. これについては非常に多くのことがあるようですが、どの行が何をするか、どこから来るか、何をするかなどについて、順を追って説明するセクションはありません。今2つのタスクに...

カメラを使用して画像を撮り、ファイルを出力として何らかの方法で提供します...次に、これをbase64文字列としてDBなどに保存します。

通常、バイナリ データを DB に保存すると、クエリが遅くて苦痛になる可能性があることは理解していますが、一度に表示される画像はわずかしかないため、ここであまり問題を起こすべきではありません。SQLite クエリは小さいです。

まず、私はAndroidをフォローしていました:カメラで写真を撮り、ビットマップをバイト配列に変換してsqlite dbに保存する方法は? ただし、画像をローカルに保存してファイル パスを参照する手順は、実際には機能しません。それで私はほとんど振り出しに戻った...

カメラについて学ぶためにhttp://developer.android.com/guide/topics/media/camera.htmlを読んでいます... 混乱した.com !!!

公平を期すために、私はたくさん読みましたが、今ではほとんど理解していないので、今すぐダミーガイドが必要です. これについて読んだすべての役に立たないがらくたを捨てて、最初からやり直すことができたらいいのにと思います... あなたの脳にとって、フォーマット /F はどこにありますか?

OK ということで、また始めました... オーディオも追加したので、状況はかなり良くなりましたが、それについては説明しません。現在、私のアプリは写真を撮り、(標準のカメラ インテントを使用して) プレビューし、[保存] をクリックするとアプリにローカルに表示されます。画像を表示する代わりに、[保存] をクリックすると、blob/bit64 としてデータベースに保存する必要があります

ここに私のコードがあるので、私がどこにいるかを見ることができます...

これを画像を取得するための呼び出しとして使用しようとしています...

 <?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);
}

}

トリックを行うようですが、DB セクション用に編集する必要があります。「画像をByte []に​​変換して保存し、sqliteでBlobとして保存する」必要があります-またはそう信じています...これを行う最初のステップは何ですか? 画像ビューをもう表示したくないことに注意してください。代わりに、一意の ID/整数で DB に保存します。

どんなポインタでも素晴らしいでしょう!!

4

1 に答える 1

0

base64 を使用してそれを行うことはできないと思いますが、画像を Byte[] に変換し、sqlite で Blob として保存することでそれを行うことができます。

于 2013-07-05T09:12:33.720 に答える