如何从相机拍照并在Android应用程序中添加照片库?

时间:2012-05-02 05:38:12

标签: android sqlite gallery

我是android deveoper。我需要帮助,我尝试从相机拍照,我得到照片,以及如何添加画廊视图。如何可能吗?我的代码在下面!! enter code here

谢谢!

send_zoo_btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            String base64string = "";
            // bitmap = i.getDrawingCache();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            ImageByte = stream.toByteArray();
            base64string = Base64.encodeBytes(ImageByte);

            db.execSQL("insert into newanimal(path,selece_animal) values('"
                    + base64string + "','" + id + "' )");
            Toast.makeText(ThirdActivity.this, "Data inserted", 1).show();
            Intent intent = new Intent().setClass(ThirdActivity.this,
                    ZoobuzzActivity.class);
            startActivity(intent);
        }
    });
}

中我的onactivity结果方法下面
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("CheckStartActivity", "onActivityResult and resultCode = "
            + requestCode);
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY || requestCode == CAMERA) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA) {
                Log.d("log", " request code camera " + requestCode);
                final File file = getTempFile(this);
                try {
                    selImagePath = file.getAbsolutePath();
                    Log.v("anim", "path  " + selImagePath);

                    m_bmOCRBitmap = Media.getBitmap(getContentResolver(),
                            Uri.fromFile(file));

                    int calwidth;
                    int calheight;

                    // OI FILE Manager
                    FileOutputStream out;

                    out = new FileOutputStream(file);
                    int width = m_bmOCRBitmap.getWidth();
                    int height = m_bmOCRBitmap.getHeight();
                    if (width > height) {
                        calwidth = (int) ((width * 250) / height);
                        calheight = 250;
                    } else {
                        calheight = (int) ((height * 200) / width);
                        calwidth = 200;
                    }

                    bitmap = Bitmap.createScaledBitmap(m_bmOCRBitmap,
                            calwidth, calheight, true);

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

                    Log.v("bitmap", " bitmap :" + bitmap);

                    img_pht.setImageBitmap(bitmap);

                    hideMenu();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            } else if (requestCode == GALLERY) {

                Uri selectedImageUri = data.getData();

                Log.v("", " result code : " + resultCode + " request "
                        + requestCode + " data " + data);

                realpath = getPath(selectedImageUri);

                Log.v("anim", " realpath : " + realpath);
                file = new File(realpath);

                Log.v("anim", " file : " + file);

                m_bmOCRBitmap = BitmapFactory.decodeFile(realpath);
                int calwidth;
                int calheight;

                // OI FILE Manager
                FileOutputStream out;
                try {
                    out = new FileOutputStream(file);
                    int width = m_bmOCRBitmap.getWidth();
                    int height = m_bmOCRBitmap.getHeight();
                    if (width > height) {
                        calwidth = (int) ((width * 250) / height);
                        calheight = 250;
                    } else {
                        calheight = (int) ((height * 200) / width);
                        calwidth = 200;
                    }

                    bitmap = Bitmap.createScaledBitmap(m_bmOCRBitmap,
                            calwidth, calheight, true);

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

                    Log.v("bitmap", " bitmap :" + bitmap);

                    img_pht.setImageBitmap(bitmap);
                    hideMenu();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }
    if (resultCode == 1) {
        Log.d("log", " request code di= " + requestCode);

        id = data.getExtras().getInt("id");
        if (GViewAdapter.imageIDs.get(id).equals("R.drawable")) {
            int as = Integer.parseInt("" + GViewAdapter.imageIDs.get(id));
            img_chose.setImageResource(as);
        } else {

        }

        Log.v("log", " id" + data.getExtras().getInt("id"));

    }

}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

private File getTempFile(Context context) {
    final File path = new File(Environment.getExternalStorageDirectory(),
            context.getPackageName());
    if (!path.exists()) {
        path.mkdir();
    }
    return new File(path, fileName);
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (!(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT)
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}

public void onBackPressed() {
    if (isMenuVisible) {
        hideMenu();
    } else {
        finish();
    }
}

错误

05-02 11:26:35.900: ERROR/AndroidRuntime(510): FATAL EXCEPTION: main
05-02 11:26:35.900: ERROR/AndroidRuntime(510): java.lang.ArrayIndexOutOfBoundsException
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.zoobuzz.FirstActivity$1.onItemClick(FirstActivity.java:39)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.widget.Gallery.onSingleTapUp(Gallery.java:864)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:557)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.widget.Gallery.onTouchEvent(Gallery.java:839)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.View.dispatchTouchEvent(View.java:3766)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.os.Looper.loop(Looper.java:123)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-02 11:26:35.900: ERROR/AndroidRuntime(510):     at dalvik.system.NativeStart.main(Native Method)


enter code here

1 个答案:

答案 0 :(得分:0)

使用

调用相机意图
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(intent, PICK_IMAGE_FROM_CAMERA);



@Override
protected void onActivityResult(int requestCode , int resultCode ,
        Intent data) {

    // TODO Auto-generated method stub


    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case PICK_IMAGE_FROM_CAMERA:

                Uri uri = data.getData();
                imageview.setImageURI(uri);

                break;

}
    }
}

将图像保存在特定位置的SD卡上

if (uri != null) {
        String SD_Card_Path = Environment.getExternalStorageDirectory()
                .toString();
        String Our_Path = SD_Card_Path + "/NewFolder/Images/";
        OutputStream fOut = null;
        ImageFullName = Image_Name + "_" + System.currentTimeMillis()
                + ".jpg";
        File file = new File(Our_Path + ImageFullName);
        try {
            fOut = new FileOutputStream(file);

            Bitmap mBitmap = null;
            mBitmap = Media.getBitmap(this.getContentResolver(),
                    myCaptureUri);

            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }