如何调用android系统摄像头并将图片存储在某个文件夹中?

如何调用android系统摄像头并将图片存储在某个文件夹中?,android,camera,android-intent,Android,Camera,Android Intent,我想调用系统摄像头,在系统视图中拍摄大量照片,我还想将这些照片存储在一些文件夹中。我搜索了一些代码,如下所示: Intent imageCaptureIntent = new Intent("android.media.action.STILL_IMAGE_CAMERA"); File out = new File(Environment.getExternalStorageDirectory(), "camera"+System.currentTime

我想调用系统摄像头,在系统视图中拍摄大量照片,我还想将这些照片存储在一些文件夹中。我搜索了一些代码,如下所示:

            Intent imageCaptureIntent = new Intent("android.media.action.STILL_IMAGE_CAMERA"); 
        File out = new File(Environment.getExternalStorageDirectory(), "camera"+System.currentTimeMillis()+".jpg");
        Uri uri = Uri.fromFile(out);
        imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(imageCaptureIntent, 1);
这段代码可以让我在系统视图中拍摄大量照片,但它无法存储我想要的文件夹(Environment.getExternalStorageDirectory()+“camera”+system.currentTimeMillis()+“.jpg”)。所以,谁有好办法确保我的照片存储在我想要的文件夹中呢?谢谢

在onCreate()中执行此操作

在onActivityResult中

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);

      if (resultCode != RESULT_OK)
          return;

      try {
        AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
        FileInputStream fis = videoAsset.createInputStream();
        File tmpFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); 
        FileOutputStream fos = new FileOutputStream(tmpFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = fis.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }       
        fis.close();
        fos.close();
      } catch (IOException io_e) {
        // TODO: handle error
      }
}
在onCreate()中,执行以下操作:

在onActivityResult中

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);

      if (resultCode != RESULT_OK)
          return;

      try {
        AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
        FileInputStream fis = videoAsset.createInputStream();
        File tmpFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg"); 
        FileOutputStream fos = new FileOutputStream(tmpFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = fis.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }       
        fis.close();
        fos.close();
      } catch (IOException io_e) {
        // TODO: handle error
      }
}

在上面的代码中,不创建文件夹,而是将其存储在默认文件夹中

但此代码是存储在特定文件夹中的图像

 public class MainActivity extends ActionBarActivity {

        Button btnImage;
        ImageView imageView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            imageView= (ImageView) findViewById(R.id.imageView);
            btnImage= (Button) findViewById(R.id.buttonCapture);
            btnImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, 0);
                }
            });

        }
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);

            if (resultCode != RESULT_OK)
                return;

            try {
                AssetFileDescriptor imageAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
                FileInputStream fis = imageAsset.createInputStream();

                File root=new File(Environment.getExternalStorageDirectory(),"/Imagesss/");//Folder Name Imagesss
                if (!root.exists()) {
                    System.out.println("No directory");
                    root.mkdirs();
                }

                File file;
                file=new File(root,"android_"+System.currentTimeMillis()+".jpg" );

                FileOutputStream fos = new FileOutputStream(file);

                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    fos.write(buf, 0, len);
                }
                fis.close();
                fos.close();


            } catch (IOException io_e) {
                // TODO: handle error
            }

            //Display Data In Image View
            if (requestCode == 0 && resultCode == RESULT_OK) {
                Bundle extras = intent.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                imageView.setImageBitmap(imageBitmap);
            }
        }

在上面的代码中,不创建文件夹,而是将其存储在默认文件夹中

但此代码是存储在特定文件夹中的图像

 public class MainActivity extends ActionBarActivity {

        Button btnImage;
        ImageView imageView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            imageView= (ImageView) findViewById(R.id.imageView);
            btnImage= (Button) findViewById(R.id.buttonCapture);
            btnImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, 0);
                }
            });

        }
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);

            if (resultCode != RESULT_OK)
                return;

            try {
                AssetFileDescriptor imageAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
                FileInputStream fis = imageAsset.createInputStream();

                File root=new File(Environment.getExternalStorageDirectory(),"/Imagesss/");//Folder Name Imagesss
                if (!root.exists()) {
                    System.out.println("No directory");
                    root.mkdirs();
                }

                File file;
                file=new File(root,"android_"+System.currentTimeMillis()+".jpg" );

                FileOutputStream fos = new FileOutputStream(file);

                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    fos.write(buf, 0, len);
                }
                fis.close();
                fos.close();


            } catch (IOException io_e) {
                // TODO: handle error
            }

            //Display Data In Image View
            if (requestCode == 0 && resultCode == RESULT_OK) {
                Bundle extras = intent.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                imageView.setImageBitmap(imageBitmap);
            }
        }

简单背压事件我相信简单背压事件我相信