安卓如何将图片保存到SD卡?

7

更新

添加了

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

现在将它添加到清单中即可正常工作。

好的,我创建了一个应用程序,最终希望能够拍照,然后进入另一个屏幕,让您可以“使用”或“重新拍摄”照片。

当拍摄图像时,需要将其保存到SD卡上的新文件夹中(如果该文件夹不存在,则需要创建它)。几周前我已经完成了所有这些工作,但是在编辑并关闭eclipse之后,我似乎无法让它重新工作?

此部分位于int imageNum = 0;之后,我添加了imagesFolder.mkdirs();,我相信这是正确的创建新文件夹,但现在似乎甚至这个也不起作用。

现在只会拍摄图像,而不会创建新文件夹或保存图像。

public class AndroidCamera extends Activity implements SurfaceHolder.Callback {

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

final int RESULT_SAVEIMAGE = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);

    Button buttonTakePicture = (Button) findViewById(R.id.takepicture);
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            camera.takePicture(myShutterCallback, myPictureCallback_RAW,
                    myPictureCallback_JPG);

        }
    });
}

ShutterCallback myShutterCallback = new ShutterCallback() {

    public void onShutter() {
        // TODO Auto-generated method stub

    }
};

PictureCallback myPictureCallback_RAW = new PictureCallback() {

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub

    }
};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */


        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "BeatEmUp");
        imagesFolder.mkdirs(); 
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        }
        Uri uriSavedImage = Uri.fromFile(output);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);


        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidCamera.this, 
                    "Image saved: ", 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        camera.startPreview();
    }};


public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if (previewing) {
        camera.stopPreview();
        previewing = false;
    }

    if (camera != null) {
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

    camera = Camera.open();
    try {
        Camera.Parameters parameters = camera.getParameters();
        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            // This is an undocumented although widely known feature
            parameters.set("orientation", "portrait");
            // For Android 2.2 and above
            camera.setDisplayOrientation(90);
            // Uncomment for Android 2.0 and above
            parameters.setRotation(90);
        } else {
            // This is an undocumented although widely known feature
            parameters.set("orientation", "landscape");
            // For Android 2.2 and above
            camera.setDisplayOrientation(0);
            // Uncomment for Android 2.0 and above
            parameters.setRotation(0);
        }
        camera.setParameters(parameters);
        camera.setPreviewDisplay(holder);
    } catch (IOException exception) {
        camera.release();

    }
    camera.startPreview();

}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;
}
}

2
你是否遇到任何错误?你的清单中是否列出了写入SD卡的权限? - Dave Swersky
谢谢,愚蠢的错误是没有在清单文件中添加权限 :) - Matt
你想把这个作为答案添加吗?如果可以的话,我会接受它,这可能会帮助其他用户。 - Matt
1个回答

8

您应该确保清单中列出了写入SD卡的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接