簡體   English   中英

如何調用android系統相機並將圖片存儲在某個文件夾中?

[英]How to call android system camera and store the picture in some folder?

我想調用系統攝像頭並在系統視圖中拍攝大量照片,我也想將這些照片存儲在一些文件夾中。 我搜索了一些代碼,如下所示:

            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()這樣做,

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

在上面的代碼中不創建它存儲在默認文件夾中的文件夾

但是這段代碼是特定文件夾中的商店圖片

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM