Get uri from camera intent in android

前端 未结 5 991
太阳男子
太阳男子 2020-12-10 12:27

on click

 takePic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent m_int         


        
5条回答
  •  时光说笑
    2020-12-10 12:56

    First of all make sure that your directory is created......

    final String dir =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/Folder/";
         File newdir = new File(dir); 
         newdir.mkdirs();
    

    on button click call this function.

    private void capturarFoto() {
     String file = dir+DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString()+".jpg";
    
    
    File newfile = new File(file);
                try {
                    newfile.createNewFile();
                } catch (IOException e) {}       
    
                Uri outputFileUri = Uri.fromFile(newfile);
    
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    
                startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("Demo Pic", "Picture is saved");
    
    
        }
    }
    

    Make sure You add permission in manifest

    
    
    
    

提交回复
热议问题