📌  相关文章
📜  如何将捕获的图像共享到 Android 中的另一个应用程序

📅  最后修改于: 2022-05-13 01:55:22.586000             🧑  作者: Mango

如何将捕获的图像共享到 Android 中的另一个应用程序

先决条件:如何通过Intent打开Camera并抓图

在本文中,我们将尝试使用 Android Studio 将捕获的图像(来自本文)发送到其他应用程序。

方法:

  1. 捕获的图像存储在外部存储中。因此,我们需要向用户请求访问文件的权限。所以在manifest文件中获取访问外部存储权限的权限。
  2. 这里的pictureDir(File)指向的是名为DIRECTORY_PICTURES的外部存储目录
    File pictureDir
        = new File(
            Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),
            "CameraDemo");
    
  3. 在 onCreate() 方法中,检查目录pictureDir是否存在。如果没有,则使用以下代码创建目录
    if(!pictureDir.exists()){
        pictureDir.mkdirs();
    }
    
  4. 创建另一个名为callCameraApp()的方法以从外部存储中获取单击的图像。
    • 使用 Intent 捕获图像
    • 创建一个文件将图像存储在pictureDir 目录中。
    • 获取此图像文件的 URI 对象
    • 将图像放在 Intent 存储上,以便从应用程序的其他模块访问。
    • 通过意图将图像传递给 startActivityForResult()
  5. 使用意图将此图像共享给其他应用程序。
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    
  6. 在本文中,我们将选择 Gmail,并将此图像作为邮件附件发送。
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    

下面是上述方法的完整实现:

activity_main.xml


  
         
    
  
     
    


MainActivity.java
package com.example.camera_mail;
  
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
  
import java.io.File;
  
public class MainActivity
    extends AppCompatActivity
    implements View.OnClickListener {
  
    private static final int
        CAMERA_PIC_REQUEST
        = 1337;
    private static final int
        REQUEST_EXTERNAL_STORAGE_RESULT
        = 1;
    private static final String
        FILE_NAME
        = "image01.jpg";
  
    private Button b1;
    private ImageView img1;
  
    File pictureDir
        = new File(
            Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),
            "CameraDemo");
    private Uri fileUri;
  
    // The onCreate() method
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        b1 = (Button)findViewById(R.id.button1);
        img1 = (ImageView)findViewById(R.id.imageView1);
        b1.setOnClickListener(this);
        if (!pictureDir.exists()) {
            pictureDir.mkdirs();
        }
    }
  
    // Open the camera app to capture the image
    public void callCameraApp()
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File image = new File(pictureDir, FILE_NAME);
        fileUri = Uri.fromFile(image);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }
  
    public void onClick(View arg0)
    {
        if (
            ContextCompat.checkSelfPermission(
                this,
                Manifest
                    .permission
                    .WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
  
            callCameraApp();
        }
        else {
            if (
                ActivityCompat
                    .shouldShowRequestPermissionRationale(
                        this,
                        Manifest
                            .permission
                            .WRITE_EXTERNAL_STORAGE)) {
                Toast.makeText(
                         this,
                         "External storage permission"
                             + " required to save images",
                         Toast.LENGTH_SHORT)
                    .show();
            }
  
            ActivityCompat
                .requestPermissions(
                    this,
                    new String[] {
                        Manifest
                            .permission
                            .WRITE_EXTERNAL_STORAGE },
                    REQUEST_EXTERNAL_STORAGE_RESULT);
        }
    }
  
    protected void onActivityResult(int requestCode,
                                    int resultCode,
                                    Intent data)
    {
  
        if (requestCode == CAMERA_PIC_REQUEST
            && resultCode == RESULT_OK) {
            ImageView imageView
                = (android.widget.ImageView)
                    findViewById(R.id.imageView1);
  
            File image = new File(pictureDir, FILE_NAME);
            fileUri = Uri.fromFile(image);
            imageView.setImageURI(fileUri);
            emailPicture();
        }
        else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(
                     this,
                     "You did not click the photo",
                     Toast.LENGTH_SHORT)
                .show();
        }
    }
  
    @Override
    public void onRequestPermissionsResult(
        int requestCode,
        String[] permissions,
        int[] grantResults)
    {
  
        if (requestCode == REQUEST_EXTERNAL_STORAGE_RESULT) {
            if (grantResults[0]
                == PackageManager.PERMISSION_GRANTED) {
                callCameraApp();
            }
            else {
                Toast.makeText(
                         this, "External write permission"
                                   + " has not been granted, "
                                   + " cannot saved images",
                         Toast.LENGTH_SHORT)
                    .show();
            }
        }
        else {
            super.onRequestPermissionsResult(
                requestCode,
                permissions,
                grantResults);
        }
    }
  
    // Function to send the image through mail
    public void emailPicture()
    {
        Toast.makeText(
                 this,
                 "Now, sending the mail",
                 Toast.LENGTH_LONG)
            .show();
  
        Intent emailIntent
            = new Intent(
                android.content.Intent.ACTION_SEND);
        emailIntent.setType("application/image");
  
        emailIntent.putExtra(
            android.content.Intent.EXTRA_EMAIL,
            new String[] {
  
                // default receiver id
                "enquiry@geeksforgeeks.org" });
  
        // Subject of the mail
        emailIntent.putExtra(
            android.content.Intent.EXTRA_SUBJECT,
            "New photo");
  
        // Body of the mail
        emailIntent.putExtra(
            android.content.Intent.EXTRA_TEXT,
            "Here's a captured image");
  
        // Set the location of the image file
        // to be added as an attachment
        emailIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
  
        // Start the email activity
        // to with the prefilled information
        startActivity(
            Intent.createChooser(emailIntent,
                                 "Send mail..."));
    }
}


AndroidManifest.xml


  
    
  
    
        
            
                
  
                
            
        
    
  


  1. 启动应用程序
    应用界面

    启动应用程序

  2. 捕捉图像
    单击图像并向用户提供两个选项

    捕获的图像并为用户提供两个选项

  3. 选择要共享捕获的图像的应用程序。这里选择了GMail
  4. 通过邮件发送捕获的图像

    通过邮件发送捕获的图像

  5. 收到的图像

    通过邮件收到的图像