Как позвонить в системную камеру android и сохранить картинку в какой-то папке? - PullRequest
1 голос
/ 28 июля 2011

Я хочу вызвать системную камеру и сделать много снимков в системном представлении, а также сохранить эти снимки в некоторых папках.У меня есть поиск некоторых кодов, как показано ниже:

            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 "). Итак, у кого есть хороший способ убедиться, что мое фото хранится в папке, которую я хочу. Спасибо вам заранее!

Ответы [ 2 ]

3 голосов
/ 28 июля 2011

В вашем onCreate () сделайте это,

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    startActivityForResult(cameraIntent, TAKE_PICTURE_WITH_CAMERA);

И в 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
      }
}
1 голос
/ 28 февраля 2015

в приведенном выше коде не создает папку, она хранится в папке по умолчанию

, но этот код является изображением хранилища в определенной папке

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