【问题标题】:Android: How do I save captured image(s) to a specific folderAndroid:如何将捕获的图像保存到特定文件夹
【发布时间】:2014-08-05 09:51:09
【问题描述】:

我是 Android 编程的新手,我无法弄清楚如何将图像保存到特定文件夹。假设我有一个名为“myCapturedImages”的文件夹,我会将其保存在此文件夹/目录中。该目录位于Internal Storage\Pictures\myCapturedImages。我拍摄的图片大小为 0 字节,无法打开。我认为主要问题是我的 onPictureTaken 函数。关于我应该如何实现预期的保存目录的任何线索?

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

【问题讨论】:

    标签: android android-camera android-file


    【解决方案1】:

    获取图像的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);
    }
    

    【讨论】:

      【解决方案2】:

      这是启动相机并将图片保存到特定文件夹的简单程序...

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

      确保您已在清单文件中添加了 WRITE_EXTERNAL STORAGE 和 CAMERA 权限。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-08
        • 2013-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多