1

我已经参考了捕获图像并将其保存到 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 可以建议我如何将捕获的图像保存在数据库中,那对我来说将是另一个优势。

提前致谢..

4

4 回答 4

2

你可以像这样在按钮上启动相机..

ButtonClick2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent cameraIntent = new                                     Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST);  

                }
            });





and then..

protected void onActivityResult(int requestCode, int resultcode, Intent intent)
  {
      super.onActivityResult(requestCode, resultcode, intent);

      if (requestCode == CAMERA_REQUEST) {  

          Bitmap photo = (Bitmap) intent.getExtras().get("data"); 
          image1.setImageBitmap(bitmap);


      }  


  }

我想这可能会对你有所帮助。

于 2012-07-11T13:21:24.080 回答
1

您应该查看Google 培训,以获取从 SD 卡存储和读取的全尺寸图像,以及将图像存储到数据库使用SQLite数据库,您需要在其中存储文件所在的地址。当您希望显示图片时,请阅读数据库并使用谷歌培训中的代码从 SD 获取图片。

希望它有所帮助。

于 2012-07-11T13:51:37.083 回答
0

我认为当您捕获图像时,有两个选项“保存”和“丢弃”,单击保存按钮然后它会自动保存在 sdcard 中。

于 2012-07-12T04:27:09.663 回答
0

此代码用于将图像保存在数据库中。

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
            camaraBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] data = baos.toByteArray();

            for(int i=0;i<yourCursorLastPosition;i++){
            cursor.moveToPosition(i);   
            ContentValues cv = new ContentValues(imageColumnNo);

            cv.put(MyDbHelper.COL_IMG, data);

            mdb.insert(MyDbHelper.TABLE_NAME, null, cv);
            }
于 2012-07-12T06:18:40.710 回答