Android 图片未保存在设备中

Android 图片未保存在设备中,android,eclipse,image,save,Android,Eclipse,Image,Save,我不知道我做错了,一切似乎都很好,但当我按下“捕获”键时,手机中没有保存任何图片 “图片已保存”消息出现,但“我的图库”中没有任何内容 package com.mamlambo.mirror; import java.io.File; import java.io.FileOutputStream; import com.mamlambo.mirror.R; import android.annotation.SuppressLint; import android.app.Activity;

我不知道我做错了,一切似乎都很好,但当我按下“捕获”键时,手机中没有保存任何图片

“图片已保存”消息出现,但“我的图库”中没有任何内容

package com.mamlambo.mirror;

import java.io.File;
import java.io.FileOutputStream;
import com.mamlambo.mirror.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MirrorActivity extends Activity implements PictureCallback {
private final static String DEBUG_TAG = "MirrorActivity";
private Camera mCam;
private MirrorView mCamPreview;
private int mCameraId = 0;
private FrameLayout mPreviewLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // do we have a camera?
    if (!getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Toast.makeText(this, "No camera feature on this device",
                Toast.LENGTH_LONG).show();
    } else {

        mCameraId = findFirstFrontFacingCamera();

        if (mCameraId >= 0) {
            mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
            mPreviewLayout.removeAllViews();

            startCameraInLayout(mPreviewLayout, mCameraId);

            Button takePic = (Button) findViewById(R.id.capture);
            takePic.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    mCam.takePicture(null, null, MirrorActivity.this);

                }
            });
        } else {
            Toast.makeText(this, "No front facing camera found.",
                    Toast.LENGTH_LONG).show();
        }
    }
}

@SuppressLint("NewApi")
private int findFirstFrontFacingCamera() {
    int foundId = -1;
    // find the first front facing camera
    int numCams = Camera.getNumberOfCameras();
    for (int camId = 0; camId < numCams; camId++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(camId, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            Log.d(DEBUG_TAG, "Found front facing camera");
            foundId = camId;
            break;
        }
    }
    return foundId;
}

private void startCameraInLayout(FrameLayout layout, int cameraId) {

    // TODO pull this out of the UI thread.
    mCam = Camera.open(cameraId);
    if (mCam != null) {
        mCamPreview = new MirrorView(this, mCam);
        layout.addView(mCamPreview);
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (mCam == null && mPreviewLayout != null) {
        mPreviewLayout.removeAllViews();
        startCameraInLayout(mPreviewLayout, mCameraId);
    }
}

@Override
protected void onPause() {
    if (mCam != null) {
        mCam.release();
        mCam = null;
    }
    super.onPause();
}

public void onPictureTaken(byte[] data, Camera camera) {
    File pictureFileDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "SimpleSelfCam");

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

        Log.d(DEBUG_TAG, "Can't create directory to save image");
        Toast.makeText(this, "Can't make path to save pic.",
                Toast.LENGTH_LONG).show();
        return;

    }

    String filename = pictureFileDir.getPath() + File.separator
            + "latest_mug.jpg";
    File pictureFile = new File(filename);

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
        Toast.makeText(this, "Image saved as latest_mug.jpg",
                Toast.LENGTH_LONG).show();
    } catch (Exception error) {
        Log.d(DEBUG_TAG, "File not saved: " + error.getMessage());
        Toast.makeText(this, "Can't save image.", Toast.LENGTH_LONG).show();
    }
}

public class MirrorView extends SurfaceView implements
        SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public MirrorView(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (Exception error) {
            Log.d(DEBUG_TAG,
                    "Error starting mPreviewLayout: " + error.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w,
            int h) {
        if (mHolder.getSurface() == null) {
            return;
        }

        // can't make changes while mPreviewLayout is active
        try {
            mCamera.stopPreview();
        } catch (Exception e) {

        }

        try {
            // set rotation to match device orientation
            setCameraDisplayOrientationAndSize();

            // start up the mPreviewLayout
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception error) {
            Log.d(DEBUG_TAG,
                    "Error starting mPreviewLayout: " + error.getMessage());
        }
    }

    public void setCameraDisplayOrientationAndSize() {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(mCameraId, info);
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        int degrees = rotation * 90;

        /*
         * // the above is just a shorter way of doing this, but could break
         * if the values change switch (rotation) { case Surface.ROTATION_0:
         * degrees = 0; break; case Surface.ROTATION_90: degrees = 90;
         * break; case Surface.ROTATION_180: degrees = 180; break; case
         * Surface.ROTATION_270: degrees = 270; break; }
         */

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;
        } else {
            result = (info.orientation - degrees + 360) % 360;
        }
        mCamera.setDisplayOrientation(result);

        Camera.Size previewSize = mCam.getParameters().getPreviewSize();
        if (result == 90 || result == 270) {
            // swap - the physical camera itself doesn't rotate in relation
            // to the screen ;)
            mHolder.setFixedSize(previewSize.height, previewSize.width);
        } else {
            mHolder.setFixedSize(previewSize.width, previewSize.height);

        }
      }

    }
}
package com.mamlambo.mirror;
导入java.io.File;
导入java.io.FileOutputStream;
导入com.mamlambo.mirror.R;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.content.Context;
导入android.content.pm.PackageManager;
导入android.hardware.Camera;
导入android.hardware.Camera.CameraInfo;
导入android.hardware.Camera.PictureCallback;
导入android.os.Bundle;
导入android.os.Environment;
导入android.util.Log;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
导入android.view.view;
导入android.widget.Button;
导入android.widget.FrameLayout;
导入android.widget.Toast;
公共类MirrorActivity扩展活动实现PictureCallback{
私有最终静态字符串DEBUG_TAG=“MirrorActivity”;
专用摄像机;
私人审查;
私有int-mCameraId=0;
私有框架布局mPreviewLayout;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//我们有照相机吗?
如果(!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_摄像头)){
Toast.makeText(这是“此设备上没有摄像头功能”,
Toast.LENGTH_LONG).show();
}否则{
mCameraId=findFirstFrontFacingCamera();
如果(mCameraId>=0){
mPreviewLayout=(框架布局)findViewById(R.id.camPreview);
mPreviewLayout.removeAllViews();
startCameraInLayout(mCameraId的mPreviewLayout);
按钮takePic=(按钮)findViewById(R.id.capture);
takePic.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
takePicture(null,null,MirrorActivity.this);
}
});
}否则{
Toast.makeText(此“未找到前向摄像头”,
Toast.LENGTH_LONG).show();
}
}
}
@SuppressLint(“新API”)
专用int findFirstFrontFacingCamera(){
int foundId=-1;
//找到第一个前向摄像头
int numCamas=Camera.getNumberOfCameras();
对于(int-camId=0;camId