【问题标题】:How we can click photos in background on button click in android? [closed]我们如何在android中单击按钮时单击背景中的照片? [关闭]
【发布时间】:2021-08-10 08:37:40
【问题描述】:

我有关于考试的学生应用程序,所以我想在考试开始时点击他/她的照片,我怎样才能获得学生的照片?

【问题讨论】:

    标签: android android-layout service android-camera background-process


    【解决方案1】:
    public class CameraDemoActivity extends Activity {
    int TAKE_PHOTO_CODE = 0;
    public static int count = 0;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        // Here, we are making a folder named picFolder to store
        // pics taken by the camera using this application.
        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
        File newdir = new File(dir);
        newdir.mkdirs();
    
        Button capture = (Button) findViewById(R.id.btnCapture);
        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    
                // Here, the counter will be incremented each time, and the
                // picture taken by camera will be stored as 1.jpg,2.jpg
                // and likewise.
                count++;
                String file = dir+count+".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("CameraDemo", "Pic saved");
        }
    }
    

    }

    【讨论】:

      【解决方案2】:

      试试这个解决方案:

      File dir = new File(Environment.getExternalStorageDirectory() + "/picFolder/");
          if (Build.VERSION.SDK_INT >= 29) {
              dir = 
              getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES + 
              "/picFolder/");
          }
          if (!dir.exists()) {
              dir.mkdir();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多