Call cameras and photo albums

call camera

Goal: call the camera and display the result on the interface

 

step:

1. Declare an image resource path

 

private Uri imageUri ;

2. Create an ImageView in layout.xml to display pictures, and apply for sdcard permission in AndroidManifest.xml to save the photos taken

    

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

3. Create an image file in onCreate() and get the path

imageView=(ImageView)findViewById(R.id.imageView);
imageUri=Uri.fromFile(new File(getExternalCacheDir(),"test.jpg"));

 

4. In the click event of the button, start the camera and save the shooting results

Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
   intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
   startActivityForResult(intent,1);

 

5. In the onActivityResult callback method, display the photo

 

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // Parse .jpg into a bitmap object
        Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
        imageView.setImageBitmap(bitmap);
    } catch (FileNotFoundException e) {
        e.printStackTrace ();
    }
}

 

call album

1. Because the picture is in the sdcard, you need to apply for permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

2. Start the album in the main thread

Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, 1 ); // open the album

 

3. If you want to select a picture in the album and display it on the ImageView

    In systems below 4.4, use the following code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    handleImageBeforeKitKat(data);

}


private void handleImageBeforeKitKat(Intent data) {
    Are you = data.getData ();
    String imagePath = getImagePath(uri, null);
    displayImage(imagePath);
}

private String getImagePath(Uri uri, String selection) {
    String path = null ;
     // Get the real image path through Uri and selection
 Cursor cursor = getContentResolver().query(uri, null , selection, null , null );
     if (cursor != null ) {
         if (cursor.moveToFirst ()) {
            path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
        }
        cursor.close();
    }
    return path;
}

private void displayImage(String imagePath) {
    if (imagePath != null) {
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        picture.setImageBitmap(bitmap);
    } else {
        Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
    }
}

 

   On systems above 4.4, use the following code

private void handleImageOnKitKat(Intent data) {
    String imagePath = null;
    Are you = data.getData ();
    if (DocumentsContract. isDocumentUri ( this , uri)) {
         // If it is a Uri of document type, handle
 String docId = DocumentsContract. getDocumentId (uri);
         if ( "com.android.providers.media.documents" . equals(uri.getAuthority())) {
            String id = docId.split( ":" )[ 1 ]; // Parse out the id in digital format
 String selection = MediaStore.Images.Media._ID + " =" + id;
            imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
        } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
            Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
            imagePath = getImagePath(contentUri, null);
        }
    } else if ( "content" .equalsIgnoreCase(uri.getScheme())) {
         // If it is a Uri of content type, use the normal way to process
 imagePath = getImagePath(uri, null );
    } else if ( "file" .equalsIgnoreCase(uri.getScheme())) {
         // If it is a Uri of file type, just get the image path directly
 imagePath = uri.getPath();
    }
    displayImage(imagePath); // Display the image according to the image path
 }

private String getImagePath(Uri uri, String selection) {
    String path = null ;
     // Get the real image path through Uri and selection
 Cursor cursor = getContentResolver().query(uri, null , selection, null , null );
     if (cursor != null ) {
         if (cursor.moveToFirst ()) {
            path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
        }
        cursor.close();
    }
    return path;
}

private void displayImage(String imagePath) {
    if (imagePath != null) {
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        picture.setImageBitmap(bitmap);
    } else {
        Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217395&siteId=291194637