English 中文(简体)
• 如何从摄像机中摄取照片,并在应用中添加光灯?
原标题:How to take photo from camera and add photo gallery in android application?

我是甲型散射器,需要帮助,即试图从摄像机中摄取照片,并如何添加陶瓷。 我的代码如下!

感谢!

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

下表按活动结果方法分列

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
最佳回答

使用照相机

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();
        }
    }
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签