Android ACTION_IMAGE_CAPTURE Intent

后端 未结 14 2004
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 02:04

We are trying to use the native camera app to let the user take a new picture. It works just fine if we leave out the EXTRA_OUTPUT extra and returns the small B

相关标签:
14条回答
  • 2020-11-22 02:44

    You can use this code

    private Intent getCameraIntent() {
    
        PackageManager packageManager = mContext.getPackageManager();
        List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        Intent main = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        List<ResolveInfo> launchables = packageManager.queryIntentActivities(main, 0);
        if (launchables.size() == 1)
            return packageManager.getLaunchIntentForPackage(launchables.get(0).activityInfo.packageName);
        else
            for (int n = 0; n < list.size(); n++) {
                if ((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                    Log.d("TAG", "Installed Applications  : " + list.get(n).loadLabel(packageManager).toString());
                    Log.d("TAG", "package name  : " + list.get(n).packageName);
                    String defaultCameraPackage = list.get(n).packageName;
    
    
                    if (launchables.size() > 1)
                        for (int i = 0; i < launchables.size(); i++) {
                            if (defaultCameraPackage.equals(launchables.get(i).activityInfo.packageName)) {
                                return packageManager.getLaunchIntentForPackage(defaultCameraPackage);
                            }
                        }
                }
            }
        return null;
    }
    
    0 讨论(0)
  • 2020-11-22 02:49
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED)
        {
            //do not process data, I use return; to resume activity calling camera intent
            enter code here
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:50

    I created simple library which will manage choosing images from different sources (Gallery, Camera), maybe save it to some location (SD-Card or internal memory) and return the image back so please free to use it and improve it - Android-ImageChooser.

    0 讨论(0)
  • 2020-11-22 02:52

    To follow up on Yenchi's comment above, the OK button will also do nothing if the camera app can't write to the directory in question.

    That means that you can't create the file in a place that's only writeable by your application (for instance, something under getCacheDir()) Something under getExternalFilesDir() ought to work, however.

    It would be nice if the camera app printed an error message to the logs if it could not write to the specified EXTRA_OUTPUT path, but I didn't find one.

    0 讨论(0)
  • 2020-11-22 02:52

    I recommend you to follow the android trainning post for capturing a photo. They show in an example how to take small and big pictures. You can also download the source code from here

    0 讨论(0)
  • 2020-11-22 02:57

    to have the camera write to sdcard but keep in a new Album on the gallery app I use this :

     File imageDirectory = new File("/sdcard/signifio");
              String path = imageDirectory.toString().toLowerCase();
               String name = imageDirectory.getName().toLowerCase();
    
    
                ContentValues values = new ContentValues(); 
                values.put(Media.TITLE, "Image"); 
                values.put(Images.Media.BUCKET_ID, path.hashCode());
                values.put(Images.Media.BUCKET_DISPLAY_NAME,name);
    
                values.put(Images.Media.MIME_TYPE, "image/jpeg");
                values.put(Media.DESCRIPTION, "Image capture by camera");
               values.put("_data", "/sdcard/signifio/1111.jpg");
             uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values);
                Intent i = new Intent("android.media.action.IMAGE_CAPTURE"); 
    
                i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    
                startActivityForResult(i, 0); 
    

    Please note that you will need to generate a unique filename every time and replace teh 1111.jpg that I wrote. This was tested with nexus one. the uri is declared in the private class , so on activity result I am able to load the image from the uri to imageView for preview if needed.

    0 讨论(0)
提交回复
热议问题