Android的图像保存到SD卡 [英] Android Save Image to SD Card

查看:276
本文介绍了Android的图像保存到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

添加

<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>

到清单现在都工作正常。

好了,所以我有一个程序,我已经开始打造这到底我希望能够拍摄照片,然后把你带到另外一个屏幕,可让您能够使用或重考的图片

在图像又把它需要被保存到一个新的文件夹在SD卡,(如果文件夹不存在则需要创建它)。我有这一切工作几个星期前,但后,我做了一些编辑和关闭月食我似乎无法拿回来工作?

一节,这是在 INT imageNum = 0; 我增加了 imagesFolder.mkdirs(); 这我相信的是正确的创建一个新的文件夹,但即使是这种似乎并没有被现在的工作。

现在的形象只是被拿去无论是新的文件夹被创建或图像会保存。

 公共类AndroidCamera扩展活动实现SurfaceHolder.Callback {

摄像头摄像头;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
布尔previewing = FALSE;
LayoutInflater controlInflater = NULL;

最终诠释RESULT_SAVEIMAGE = 0;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    。getWindow()和setFormat(PixelFormat.UNKNOWN);
    surfaceView =(SurfaceView)findViewById(R.id.camera preVIEW);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(本);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    查看viewControl = controlInflater.inflate(R.layout.control,NULL);
    的LayoutParams layoutParamsControl =新的LayoutParams(
            LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
    this.addContentView(viewControl,layoutParamsControl);

    按钮buttonTakePicture =(按钮)findViewById(R.id.takepicture);
    buttonTakePicture.setOnClickListener(新Button.OnClickListener(){

        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            camera.takePicture(myShutterCallback,myPictureCallback_RAW,
                    myPictureCallback_JPG);

        }
    });
}

ShutterCallback myShutterCallback =新ShutterCallback(){

    公共无效onShutter(){
        // TODO自动生成方法存根

    }
};

PictureCallback myPictureCallback_RAW =新PictureCallback(){

    公共无效onPictureTaken(byte []的为arg0,摄像机ARG1){
        // TODO自动生成方法存根

    }
};

PictureCallback myPictureCallback_JPG =新PictureCallback(){

    公共无效onPictureTaken(byte []的为arg0,摄像机ARG1){
        // TODO自动生成方法存根
        / *位图bitmapPicture
            = BitmapFactory.de codeByteArray(为arg0,0,arg0.length); * /


        INT imageNum = 0;
        意图imageIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
        文件imagesFolder =新的文件(Environment.getExternalStorageDirectory(),BeatEmUp);
        imagesFolder.mkdirs();
        字符串文件名=image_+将String.valueOf(imageNum)+.JPG;
        文件输出=新的文件(imagesFolder,文件名);
        而(output.exists()){
            imageNum ++;
            文件名=image_+将String.valueOf(imageNum)+.JPG;
            输出=新的文件(imagesFolder,文件名);
        }
        乌里uriSavedImage = Uri.fromFile(输出);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage);


        的OutputStream imageFileOS;
        尝试 {
            imageFileOS = getContentResolver()openOutputStream(uriSavedImage)。
            imageFileOS.write(为arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidCamera.this,
                    图像保存,
                    Toast.LENGTH_LONG).show();

        }赶上(FileNotFoundException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        camera.start preVIEW();
    }};


公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
        INT高度){
    // TODO自动生成方法存根
    如果(previewing){
        camera.stop preVIEW();
        previewing = FALSE;
    }

    如果(相机!= NULL){
        尝试 {
            camera.set previewDisplay(surfaceHolder);
            camera.start preVIEW();
            previewing = TRUE;
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
    }
}

公共无效surfaceCreated(SurfaceHolder持有者){
    // TODO自动生成方法存根

    相机= Camera.open();
    尝试 {
        Camera.Parameters参数= camera.getParameters();
        如果(this.getResources()。getConfiguration()。方向!= Configuration.ORIENTATION_LANDSCAPE){
            //这是一个无证虽然广为人知的功能
            parameters.set(方向,肖像);
            //为Android 2.2及以上
            camera.setDisplayOrientation(90);
            //取消注释为Android 2.0及以上
            parameters.setRotation(90);
        } 其他 {
            //这是一个无证虽然广为人知的功能
            parameters.set(方向,山水);
            //为Android 2.2及以上
            camera.setDisplayOrientation(0);
            //取消注释为Android 2.0及以上
            parameters.setRotation(0);
        }
        camera.setParameters(参数);
        camera.set previewDisplay(保持器);
    }赶上(IOException异常除外){
        camera.release();

    }
    camera.start preVIEW();

}

公共无效surfaceDestroyed(SurfaceHolder持有者){
    // TODO自动生成方法存根
    camera.stop preVIEW();
    camera.release();
    摄像头= NULL;
    previewing = FALSE;
}
}
 

解决方案

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

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

UPDATE

Added

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

to the manifest all works ok now.

Ok so I have a app I have started to create which in the end I want to be able to take a picture, which then takes you to another screen which lets you be able to "Use" or "Retake" the picture.

When the image is took it needs to be saved into a new folder on the SD Card, (if the folder is not there then it needs to create it). I had all this working a couple of weeks ago but after i did some editing and shut down eclipse I cant seem to get it back working?

The section for this is after int imageNum = 0; i have added imagesFolder.mkdirs(); which i belive is correct to create a new folder but even this seems not to be working now.

Now the image just gets took and neither the new folder gets created or the image gets saved.

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

解决方案

You should make sure the manifest lists the permission to write to the SD card:

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

这篇关于Android的图像保存到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆