使用前置摄像头时,不会保存Pic

时间:2014-08-05 22:38:30

标签: android camera

我拍照时,当我用后置摄像头拍摄时,没问题。 但是当我选择前置摄像头时,我的拇指都是黑色的。

在我的logcat中,我有这个:

Failed to create thumbnail, removing original android

我使用的是内置相机应用程序。 我应该怀疑我需要开发自己的相机应用程序,但我记得在使用PhoneGap应用程序时我可以使用前置摄像头。

这是我的代码:

启动意图:

                imageFileUri = getContentResolver().insert(
                    Media.EXTERNAL_CONTENT_URI, new ContentValues());
            Intent i = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    imageFileUri);
            startActivityForResult(i, code);

保存图片:

if (resultCode == RESULT_OK) {
            // getGPSFixAndUpload(TAKE_PIC_INI);
            action = TAKE_PIC_INI;
            picName = getPicName(action, 0, 0, 0);
    SavePic sp = new SavePic(ctx, imageFileUri, picName, action);
    sp.execute();


// Class SavePic
@Override
    protected Bitmap doInBackground(Void... params) {
        if (mAction == TAKE_PIC_INI || mAction == TAKE_PIC_FIN) {
            bmp = ImageUtils.uri2Bitmap(ctx, imageFileUri);
            ImageUtils.saveImageToExternalCacheStorage(ctx, bmp, picName);
        }
        return bmp;
    }


public static Bitmap uri2Bitmap(Context ctx, Uri imageFileUri) {
    // int dw = 1200;
    // int dh = 1200;

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bmp = null;
    Bitmap rotatedBitmap = null;
    try {

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = 2; // To reduce memory consumation. The only risk
                                // is to have a small preview.
        InputStream is = ctx.getContentResolver().openInputStream(
                imageFileUri);
        // OutputStream outStream =
        // ctx.getContentResolver().openOutputStream(imageFileUri);

        bmp = BitmapFactory.decodeStream(is, null, o2);

        // No need to close inputStream, because we don't open it ourselves
        int imageHeight = o2.outHeight;
        int imageWidth = o2.outWidth;
        // String imageType = o2.outMimeType;

        // Rotate Bitmap
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight(), matrix, true);
        bmp.recycle();
        // Get Screen Dimension

        WindowManager wm = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;

        float factor = imageWidth / screenWidth;
        int height = Math.round(imageHeight / factor);

        // We display the image adapted to display width
        rotatedBitmap = getResizedBitmap(rotatedBitmap, screenWidth, height);

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

    return rotatedBitmap;
}


public static void saveImageToExternalCacheStorage(Context context,
        Bitmap image, String fileName) {
    String fullPath = context.getExternalCacheDir().getAbsolutePath();
    Log.e("path", fullPath);
    try {
        File dir = new File(fullPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        OutputStream fOut = null;

        File file = new File(fullPath, fileName);
        file.createNewFile();
        fOut = new FileOutputStream(file);

        image.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
        fOut.flush();
        fOut.close();

        MediaStore.Images.Media.insertImage(context.getContentResolver(),
                (String)file.getAbsolutePath(), file.getName(), file.getName());
    } catch (Exception e) {
        Log.e("saveToExternalStorage()", "problem");
        Log.e("log", e.toString());
    }
}

任何想法???

1 个答案:

答案 0 :(得分:0)

我从来不知道如何解决这个问题,但我发现在谷歌培训代码中,它运作良好,所以我可以使它有效:

http://developer.android.com/training/camera/index.html

相关问题