相机 Android 应用列表视图

Camera Android App Listview

我正在创建一个 android 应用程序,它可以拍摄照片,您可以向它添加描述,例如拍摄地点,照片中有谁 etc.But 我不知道我该怎么做将其保存在一起并在另一个 Intent 中列出。

我已经创建了捕获事件

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_obstera);

    b1=(Button)findViewById(R.id.button);
    iv=(ImageView)findViewById(R.id.imageView);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
        }
    });
}

下面的代码将显示它

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap bp = (Bitmap) data.getExtras().get("data");
    iv.setImageBitmap(bp);
}

现在我正在寻找一种方法来为其添加描述并将其保存在一起,并在另一个 Intent 中列出所有已保存的图像。

有人有这方面的示例代码吗?

为此你可以使用我的另一种方式 used.When 你调用相机意图然后它会在 folder.When 下创建一个文件名你回来 onActivityResult 你会获取此文件名。

        file_image = createImageFile();
        final Intent intent = new Intent(
                "android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file_image));
        startActivityForResult(intent, GALLERY_INTENT_VIDEO);

要创建 createImageFile 你可以使用这种方式

    private File createImageFile() throws IOException {
    final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    final String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
    final File albumF = getAlbumDir();
    final File imageF = File.createTempFile(imageFileName,
            JPEG_FILE_SUFFIX, albumF);
    return imageF;
}

private File getAlbumDir() {
    File storageDir = null;

    if (Environment.MEDIA_MOUNTED.equals(Environment
            .getExternalStorageState())) {
        storageDir = mAlbumStorageDirFactory
                .getAlbumStorageDir(getAlbumName());

        if (storageDir != null) {
            if (!storageDir.mkdirs()) {
                if (!storageDir.exists()) {
                    Log.e("directory", "failed to create directory");
                    return null;
                }
            }
        }

    } else {
        Log.e(getString(R.string.app_name),
                "External storage is not mounted READ/WRITE.");
    }
    return storageDir;
}

private String getAlbumName() {
    return getString(R.string.app_name);
}

当您调用 onActivityResult 时,您将获得内容

  protected void onActivityResult(int requestCode, int resultCode, Intent data)   {
          // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            final Uri selectedImageUri = Uri.fromFile(file_image);}

此外,您还可以将所有图像列表放入您根据相册名称创建的文件夹中。

要将大量数据(如图像)插入数据库,可以使用 SQLite,将我们的图像转换为字节数组并存储到 SQLite。

这里我们看到一个例子,商店员工的详细信息就像

员工姓名

员工照片

员工年龄

对于员工姓名,我们使用文本数据类型,对于员工年龄整数 datatype.But,我们使用哪种数据类型来存储员工照片?那时我们使用 BLOB 数据类型。

insertEmpDetails()方法,以EmployeeClass为参数,将所有Employee信息存入EmployeesTable。 在这里我们可以看到在将图像插入数据库之前,我们应该将您的位图图像转换为字节数组,因为我们正在使用 Utility class getBytes() 方法。

retriveEmpDetails() 方法从数据库和 return 员工对象中检索员工详细信息。 要获取所有 Name、Photo、Age,我们使用相应的 getXXX() 方法。 在检索照片时,我们将得到 image.at 的 blob[] 数组,此时我们需要将字节数组转换为 image.for,目的是我们在 Utility class 中使用 getPhoto() 方法,它将将我们的 blob 数组转换为位图图像并重新运行位图图像。

UI布局(activity_main.xml)

此布局用于查看

员工姓名

员工照片

员工年龄

    <?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" >

    <ImageView
        android:id="@+id/ivMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EMPLOYEE_NAME" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:text="NAME" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EMPLOYEE_PHOTO" />

        <ImageView
            android:id="@+id/photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EMPLOYEE_AGE" />

        <TextView
            android:id="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:text="AGE" />
    </LinearLayout>

</LinearLayout>

Android Activity(InsertandRetriveBlobData.java)

package com.androidsurya.sqliteexample;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class InsertandRetriveBlobData extends Activity {
 private DBhelper DbHelper;

  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  DbHelper = new DBhelper(this);
  Employee employee_One = new Employee(BitmapFactory.decodeResource(
    getResources(), R.drawable.photo), "Surya", 25);
  DbHelper.open();
  DbHelper.insertEmpDetails(employee_One);
  DbHelper.close();
  employee_One = null;
  DbHelper.open();
  employee_One = DbHelper.retriveEmpDetails();
  DbHelper.close();

  TextView empname = (TextView) findViewById(R.id.name);
  empname.setText(employee_One.getName());
  ImageView empphoto = (ImageView) findViewById(R.id.photo);
  empphoto.setImageBitmap(employee_One.getBitmap());
  TextView empage = (TextView) findViewById(R.id.age);
  empage.setText("" + employee_One.getAge());

  }
}

DBhelper.java

这是数据库class 我们在这里声明

insertEmpDetails() 插入员工详细信息的方法和 retriveEmpDetails() 检索员工详细信息的方法

package com.androidsurya.sqliteexample;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBhelper {
 public static final String EMP_ID = "id";
 public static final String EMP_NAME = "name";
 public static final String EMP_AGE = "age";
 public static final String EMP_PHOTO = "photo";

  private DatabaseHelper mDbHelper;
 private SQLiteDatabase mDb;

  private static final String DATABASE_NAME = "EmployessDB.db";
 private static final int DATABASE_VERSION = 1;

  private static final String EMPLOYEES_TABLE = "Employees";

  private static final String CREATE_EMPLOYEES_TABLE = "create table "
   + EMPLOYEES_TABLE + " (" + EMP_ID
   + " integer primary key autoincrement, " + EMP_PHOTO
   + " blob not null, " + EMP_NAME + " text not null unique, "
   + EMP_AGE + " integer );";

  private final Context mCtx;

  private static class DatabaseHelper extends SQLiteOpenHelper {
  DatabaseHelper(Context context) {
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

   public void onCreate(SQLiteDatabase db) {
   db.execSQL(CREATE_EMPLOYEES_TABLE);
  }

   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   db.execSQL("DROP TABLE IF EXISTS " + EMPLOYEES_TABLE);
   onCreate(db);
  }
 }

  public void Reset() {
  mDbHelper.onUpgrade(this.mDb, 1, 1);
 }

  public DBhelper(Context ctx) {
  mCtx = ctx;
  mDbHelper = new DatabaseHelper(mCtx);
 }

  public DBhelper open() throws SQLException {
  mDb = mDbHelper.getWritableDatabase();
  return this;
 }

  public void close() {
  mDbHelper.close();
 }

  public void insertEmpDetails(Employee employee) {
  ContentValues cv = new ContentValues();
  cv.put(EMP_PHOTO, Utility.getBytes(employee.getBitmap()));
  cv.put(EMP_NAME, employee.getName());
  cv.put(EMP_AGE, employee.getAge());
  mDb.insert(EMPLOYEES_TABLE, null, cv);
 }

  public Employee retriveEmpDetails() throws SQLException {
  Cursor cur = mDb.query(true, EMPLOYEES_TABLE, new String[] { EMP_PHOTO,
    EMP_NAME, EMP_AGE }, null, null, null, null, null, null);
  if (cur.moveToFirst()) {
   byte[] blob = cur.getBlob(cur.getColumnIndex(EMP_PHOTO));
   String name = cur.getString(cur.getColumnIndex(EMP_NAME));
   int age = cur.getInt(cur.getColumnIndex(EMP_AGE));
   cur.close();
   return new Employee(Utility.getPhoto(blob), name, age);
  }
  cur.close();
  return null;
 }
}

Utility.java

效用 class 是这里的主要角色 在插入图像和从 SQLite 数据库中检索图像之前,我们使用以下这些方法。 getBytes() 将位图图像转换为字节数组并重新运行 byte[] getPhoto() 将 byte[] 转换为位图并重新运行位图图像

package com.androidsurya.sqliteexample;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;

public class Utility {
 // convert from bitmap to byte array
 public static byte[] getBytes(Bitmap bitmap) {
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bitmap.compress(CompressFormat.PNG, 0, stream);
  return stream.toByteArray();
 }

  // convert from byte array to bitmap
 public static Bitmap getPhoto(byte[] image) {
  return BitmapFactory.decodeByteArray(image, 0, image.length);
 }
}

在Android清单文件

中注册AndroidActivity
<activity
            android:name="com.androidsurya.sqliteexample.InsertandRetriveBlobData"
            android:label="@string/app_name" >