用StartActivityForResults拍摄3张照片

时间:2016-09-28 18:13:46

标签: android android-camera startactivityforresult

我很感激一些帮助,我希望在一个循环中使用startActivityForResults()在同一个Intent下拍摄三张照片,然后返回onActivityResult(),使用MarshMallow及其特定权限在三个图像视图中打印这三张照片(我认为这不是问题,应该在我的代码中解决),

问题是该应用程序采用这三张图片,并制作ArrayList或URI,但它并没有到达三张图片视图中实际打印这三张照片,

这是我的代码,提前谢谢,

-----------------
MainActivity.java
-----------------

    public class MainActivity extends AppCompatActivity {


    //private int numFotos = 1;
    //private Bitmap bitmap;
    private ArrayList <Uri> uriFiles;
    private ImageView imageView1, imageView2,imageView3;
    private Button button_takePics;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        button_takePics = (Button) findViewById(R.id.Butt_tiraFotos);
        imageView1 = (ImageView) findViewById(R.id.image_1);
        imageView2 = (ImageView) findViewById(R.id.image_2);
        imageView3 = (ImageView) findViewById(R.id.image_3);



        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            button_takePics.setEnabled(false);
            ActivityCompat.requestPermissions(this, new String[]
                    { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
                     }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 0) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                button_takePics.setEnabled(true);
            }
        }
    }


    public void takePictures(View view){

        uriFiles = new ArrayList<>();
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);




        for (int i=1; i<4; i++){

            uriFiles.add(Uri.fromFile(getOutputMediaFile()));
            intent.putParcelableArrayListExtra(MediaStore.EXTRA_OUTPUT,uriFiles);

            startActivityForResult(intent, 100);

    }


    }

    private static File getOutputMediaFile() {

        File mediaStorageDir=null;
        File formattedFile = null;


        mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "CameraDemo");

        if (!mediaStorageDir.exists()){
            if (!mediaStorageDir.mkdirs()){
                Log.d("CameraDemo", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        formattedFile= new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");

        return formattedFile;

    }

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

        if (requestCode == 100) {
            if (resultCode == RESULT_OK) {

                uriFiles = data.getParcelableArrayListExtra(MediaStore.EXTRA_OUTPUT);


                // AQUI EMPIEZA EL PROBLEMA  ////////

                imageView1.setImageURI(uriFiles.get(0));
                imageView2.setImageURI(uriFiles.get(1));
                imageView3.setImageURI(uriFiles.get(2));


            }

        }
    }


    }


-------------------
activity_main.xml
-------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.faustocheca.photochooseshare.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="18dp"
        android:layout_marginTop="18dp"
        android:textSize="18sp"
        android:text="Tira, escoge y comparte Fotos" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="Tira 3 Fotos"
        android:id="@+id/Butt_tiraFotos"
        android:onClick="takePictures"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="48dp"
        android:orientation="horizontal"
        >

        <ImageView
            android:id="@+id/image_1"
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="120dp"
            android:layout_margin="1dp"
            android:background="#feafea"/>

        <ImageView
            android:id="@+id/image_2"
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="120dp"
            android:layout_margin="1dp"
            android:background="#feafea"/>

        <ImageView
            android:id="@+id/image_3"
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="120dp"
            android:layout_margin="1dp"
            android:background="#feafea"/>


    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="Comparte"
        android:id="@+id/Butt_comparte"
        />




</LinearLayout>

1 个答案:

答案 0 :(得分:1)

这不起作用;单个startActivityForResult将导致对onActivityResult的单个调用。

ACTION_IMAGE_CAPTURE文档或系统中的其他地方没有合同,表明您可以多次发出该合同并获得一个包含3个URI的结果。

你必须循环它以便你发射一个意图,等待结果,激发第二个意图,等待结果等等。

可以能够在这样的循环中触发3个意图,但我不确定我是否相信响应的摄像头应用程序实际上会被调用3次。但即便如此,如果可行的话,你还会收到3个单独的onActivityResult调用。