我已经参考了捕获图像并将其保存到 SD 卡中。那工作正常。现在我希望图像将显示在活动中,直到我单击按钮。谁能建议我如何使它成为可能????
我在这里粘贴代码。请告诉我我在这里做错了什么..
DVCamera.class
public class DVCameraActivity extends Activity {
static Uri capturedImageUri=null;
Button ButtonClick1,ButtonClick2;
ImageView image1,image2;
int CAMERA_PIC_REQUEST = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonClick1 =(Button) findViewById(R.id.buttonClick1);
ButtonClick2 = (Button) findViewById(R.id.buttonClick2);
image2 =(ImageView) findViewById(R.id.PhotoCaptured2);
image1 =(ImageView) findViewById(R.id.PhotoCaptured1);
ButtonClick1.setOnClickListener(new OnClickListener (){
@Override
public void onClick(View view)
{
Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(), (cal.getTimeInMillis()+".jpg"));
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
capturedImageUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// request code
//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
ButtonClick2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(), (cal.getTimeInMillis()+".jpg"));
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// request code
//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
capturedImageUri = Uri.fromFile(file);
startActivityForResult(cameraIntent, 1337);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == CAMERA_PIC_REQUEST)
{
// data.getExtras()
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap( getApplicationContext().getContentResolver(), capturedImageUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image1.setImageBitmap(bitmap);
}
else if(requestCode == 1337)
{
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap( getApplicationContext().getContentResolver(), capturedImageUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image2.setImageBitmap(bitmap);
}
else
{
Toast.makeText(DVCameraActivity.this, "Picture NOt taken", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
而hre是布局文件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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/buttonClick1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Click for Photo1" />
<Button
android:id="@+id/buttonClick2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Click for Photo2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/PhotoCaptured1"
android:layout_width="150dp"
android:layout_height="200dp" />
<ImageView
android:id="@+id/PhotoCaptured2"
android:layout_width="150dp"
android:layout_height="200dp" />
</LinearLayout>
<!--
<FrameLayout
android:id="@+id/camera_preview"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
/>
-->
</LinearLayout>
如果任何 1 可以建议我如何将捕获的图像保存在数据库中,那对我来说将是另一个优势。
提前致谢..