用StartActivityForResults拍摄3张照片



我会感激一些帮助,我想在同一个Intent下拍摄三张照片startActivityForResults()在一个循环内,并得到onActivityResult()打印这三张照片在三个imageviews,与MarshMallow,及其特定的权限(我认为这不是问题,它应该在我的代码中解决),

问题是应用程序获取了这三张照片,并创建了ArrayList或uri,但它并没有真正在三个imageview中打印这三张照片,

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

-----------------
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>

这行不通;单个startActivityForResult将导致单个调用onActivityResult。

在ACTION_IMAGE_CAPTURE文档或系统中的其他地方没有合同说你可以多次发出它并获得一个带有3个uri的结果。

你必须循环它,这样你触发一个意图,等待结果,触发第二个意图,等待结果,等等

可能能够像这样在一个循环中触发3个意图,但我不确定我是否相信响应相机应用程序实际上会被调用3次。但即使如此,你会收到3个单独的onActivityResult调用,如果那工作。

相关内容

  • 没有找到相关文章

最新更新