简体   繁体   English

Android:如何将捕获的图像保存到特定文件夹

[英]Android: How do I save captured image(s) to a specific folder

I'm a total newbie in Android programming and I'm having trouble figuring how to save an image to a specific folder. 我是Android编程的新手,无法确定如何将图像保存到特定文件夹。 Let's say i have a folder called "myCapturedImages" and I would to save it in this folder/directory. 假设我有一个名为“ myCapturedImages”的文件夹,我想将其保存在此文件夹/目录中。 The directory is located at Internal Storage\\Pictures\\myCapturedImages . 该目录位于Internal Storage\\Pictures\\myCapturedImages The pictures that I took has its size of 0 bytes and cannot be opened. 我拍摄的图片大小为0字节,无法打开。 I think the main problem is my onPictureTaken function. 我认为主要问题是我的onPictureTaken函数。 Any clue on how I should achieve the expected save directory? 关于如何实现预期的保存目录的任何线索?

PictureCallback myPictureCallback_JPG = new PictureCallback(){

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

        Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());

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

            Toast.makeText(VuzixCamera.this, "Image saved: " + uriTarget.toString(), 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();
    }};

get the Bitmap of the image and For saving Image you can use this code 获取图像的Bitmap ,并且可以使用以下代码保存Image

// showedImgae is your Bitmap image // showedImgae是您的Bitmap图像

public void SaveImage(Bitmap showedImgae){

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/DCIM/myCapturedImages");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "FILENAME-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream out = new FileOutputStream(file);
        showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Toast.makeText(activityname.this, "Image Saved", Toast.LENGTH_SHORT).show();
        out.flush();
        out.close();

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

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    getApplicationContext().sendBroadcast(mediaScanIntent);
}

this is simple program to launch camera and save picture to specific folder... 这是启动相机并将图片保存到特定文件夹的简单程序...

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

Make sure that you have added WRITE_EXTERNAL STORAGE and CAMERA permissions in manifest file. 确保已在清单文件中添加了WRITE_EXTERNAL STORAGE和CAMERA权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM