简体   繁体   English

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

[英]How to take photo from camera and add photo gallery in android application?

I am android deveoper .i needed help that i try to take photo from camera and i get photo and how to add gallery view .how to possible? 我是android开发人员。我需要帮助,我尝试从相机拍摄照片,并获取照片,以及如何添加图库视图。 my code in below!! 我的代码在下面! enter code here

Thanks!! 谢谢!!

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

in below my onactivity result method in 在下面我的活动结果方法中

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

Error 错误

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

Call the Camera Intent using 使用调用相机意图

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;

}
    }
}

To save image on SD Card on a specific Location 将图像保存到特定位置的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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM