如何让用户在android中点击5张图片并在不同的图像视图中显示

如何让用户在android中点击5张图片并在不同的图像视图中显示,android,camera,Android,Camera,我正在申请照相机 在这里点击按钮,它会打开一个相机,但我想我应该能够点击5次,并保存在5个不同的图像视图。另外,单击gallery按钮,它应该会打开一个gallery,让我只选择5张图片/图像,并将它们存储在5个不同的图像视图中 谢谢你的帮助。这是我的密码 我的XML文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="ht

我正在申请照相机 在这里点击按钮,它会打开一个相机,但我想我应该能够点击5次,并保存在5个不同的图像视图。另外,单击gallery按钮,它应该会打开一个gallery,让我只选择5张图片/图像,并将它们存储在5个不同的
图像视图中

谢谢你的帮助。这是我的密码

我的XML文件:

<RelativeLayout 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"
    tools:context="com.example.test.faultexample.MainActivity">



<Button
        android:id="@+id/camerabutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Edit2"
        android:text="Camera" />

    <LinearLayout
        android:id="@+id/image_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/camerabutton"
        android:orientation="horizontal"
        android:weightSum="10">

        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <ImageView
            android:id="@+id/imageview2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <ImageView
            android:id="@+id/imageview3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <ImageView
            android:id="@+id/imageview4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

        <ImageView
            android:id="@+id/imageview5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/image_textview_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_linear"
        android:orientation="horizontal"
        android:weightSum="10">

        <TextView
            android:id="@+id/textview_imagename1"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="" />

        <TextView
            android:id="@+id/textview_imagename2"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="" />

        <TextView
            android:id="@+id/textview_imagename3"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="" />

        <TextView
            android:id="@+id/textview_imagename4"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="" />

        <TextView
            android:id="@+id/textview_imagename5"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="" />

    </LinearLayout>

<Button
        android:id="@+id/btn_select_image_frmgallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/image_textview_linear"
        android:text="select Image" />

    <Button
        android:id="@+id/btnupload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_select_image_frmgallery"
        android:paddingTop="10dp"
        android:text="UPLOAD" />

</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity{
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText Et = (EditText) findViewById(R.id.Edit1);
        final EditText Et1 = (EditText) findViewById(R.id.Edit2);
        //Et.getText().toString();
        textview_imagename1 = (TextView) findViewById(R.id.textview_imagename1);
        textview_imagename2 = (TextView) findViewById(R.id.textview_imagename2);
        textview_imagename3 = (TextView) findViewById(R.id.textview_imagename3);
        textview_imagename4 = (TextView) findViewById(R.id.textview_imagename4);
        textview_imagename5 = (TextView) findViewById(R.id.textview_imagename5);

        imageView1 = (ImageView) findViewById(R.id.imageview1);
        imageView2 = (ImageView) findViewById(R.id.imageview2);
        imageView3 = (ImageView) findViewById(R.id.imageview3);
        imageView4 = (ImageView) findViewById(R.id.imageview4);
        imageView5 = (ImageView) findViewById(R.id.imageview5);

        //Button Upload Binding
        buttonUploaad = (Button) findViewById(R.id.btnupload);
        buttonUploaad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo1 = ((BitmapDrawable) imageView1.getDrawable()).getBitmap();
                photo2 = ((BitmapDrawable) imageView2.getDrawable()).getBitmap();
                photo3 = ((BitmapDrawable) imageView3.getDrawable()).getBitmap();
                photo4 = ((BitmapDrawable) imageView4.getDrawable()).getBitmap();
                photo5 = ((BitmapDrawable) imageView5.getDrawable()).getBitmap();
                //Toast.makeText(getApplicationContext(),"Uploaded Succesfully",Toast.LENGTH_LONG).show();
                new UploadImage().execute();
            }
        });

        camerabutton = (Button) findViewById(R.id.camerabutton);
        camerabutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });

        btnselect = (Button) findViewById(R.id.btn_select_image_frmgallery);
        btnselect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_PICTURE);
            }
        });
}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PICK_PICTURE:
                if(requestCode== PICK_PICTURE)
                {
                    if(resultCode==RESULT_OK){
                        //data.getParcelableArrayExtra(name);
                        //If Single image selected then it will fetch from Gallery
                        if(data.getData()!=null){
                            Uri mImageUri=data.getData();
                        }else{
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                if(data.getClipData()!=null){
                                    ClipData mClipData=data.getClipData();
                                    ArrayList<Uri> mArrayUri=new ArrayList<Uri>();
                                    for(int i=0;i<mClipData.getItemCount();i++){
                                        ClipData.Item item = mClipData.getItemAt(i);
                                        Uri uri = item.getUri();
                                        mArrayUri.add(uri);
                                    }
                                    Log.v("LOG_TAG", "Selected Images"+ mArrayUri.size());
                                }
                            }
                        }
                    }
                }
                super.onActivityResult(requestCode, resultCode, data);
            case CAMERA_REQUEST:
                TextView textView = (TextView) findViewById(R.id.textview_imagename1);
                textView.getText();
                if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
                    photo1 = (Bitmap) data.getExtras().get("data");
                    imageView1.setImageBitmap(photo1);
                    SaveImage(photo1);
                }
        }
    }

    private void SaveImage(Bitmap finalBitmap) {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        fname = "Image-" + timeStamp + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            textview_imagename1.setText(fname);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

private class UploadImage extends AsyncTask<Void, Void, String> {
        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(MainActivity.this, "Please Wait !!!", "Please wait", true);
        }

        @Override
        protected String doInBackground(Void... params) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Must compress the Image to reduce image size to make upload easy
            photo1.compress(Bitmap.CompressFormat.PNG, 50, stream);

            byte[] byte_arr = stream.toByteArray();
            String encodedImage = Base64.encodeToString(byte_arr, Base64.DEFAULT);
return encodedImage;


        }

        @Override
        protected void onPostExecute(String encodedString) {
            if (dialog.isShowing()) {
                dialog.dismiss();
                Toast.makeText(getApplicationContext(), encodedString, Toast.LENGTH_LONG).show();
            }
        }
    }
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终EditText Et=(EditText)findViewById(R.id.Edit1);
最终EditText Et1=(EditText)findViewById(R.id.Edit2);
//Et.getText().toString();
textview_imagename1=(textview)findViewById(R.id.textview_imagename1);
textview_imagename2=(textview)findViewById(R.id.textview_imagename2);
textview\u imagename3=(textview)findViewById(R.id.textview\u imagename3);
textview_imagename4=(textview)findViewById(R.id.textview_imagename4);
textview_imagename5=(textview)findViewById(R.id.textview_imagename5);
imageView1=(ImageView)findViewById(R.id.imageView1);
imageView2=(ImageView)findViewById(R.id.imageView2);
imageView3=(ImageView)findViewById(R.id.imageView3);
imageView4=(ImageView)findViewById(R.id.imageView4);
imageView5=(ImageView)findViewById(R.id.imageView5);
//按钮上传绑定
buttonUploaad=(Button)findViewById(R.id.btnupload);
buttonUploaad.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
photo1=((BitmapDrawable)imageView1.getDrawable()).getBitmap();
photo2=((BitmapDrawable)imageView2.getDrawable()).getBitmap();
photo3=((BitmapDrawable)imageView3.getDrawable()).getBitmap();
photo4=((BitmapDrawable)imageView4.getDrawable()).getBitmap();
photo5=((BitmapDrawable)imageView5.getDrawable()).getBitmap();
//Toast.makeText(getApplicationContext(),“已成功上载”,Toast.LENGTH_LONG.show();
新建UploadImage().execute();
}
});
camerabutton=(按钮)findViewById(R.id.camerabutton);
camerabutton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
}
});
btnselect=(按钮)findViewById(R.id.btn\u select\u image\u frmgallery);
btnselect.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图=新意图();
intent.setType(“image/*”);
intent.putExtra(intent.EXTRA_允许_倍数,true);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“选择图片”),PICK_Picture;
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
开关(请求代码){
案例选取图片:
if(requestCode==选择图片)
{
if(resultCode==RESULT\u OK){
//data.getParcelableArrayExtra(名称);
//如果选择单个图像,则它将从库中提取
if(data.getData()!=null){
Uri mimageri=data.getData();
}否则{
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.JELLY\u BEAN){
if(data.getClipData()!=null){
ClipData mClipData=data.getClipData();
ArrayList mArrayUri=新的ArrayList();

对于(int i=0;i首先,您需要在您的相机上设置控件。当前它是以默认模式打开的。因此,首先在surfaceView中打开您的相机,并实现您自己的单击按钮。并根据您的要求进行处理

以下是定制相机的参考:

对于显示图像,您可以一个接一个地传递图像的uri;当您的计数完成时,您可以在需要的地方显示图像


对于gallery,您需要设置5个uri并显示它。

首先,您需要在相机上设置控件。目前它是以默认模式打开的。因此,首先在surfaceView中打开相机,并实现您自己的单击按钮。然后根据您的要求进行处理

以下是定制相机的参考:

对于显示图像,您可以一个接一个地传递图像的uri;当您的计数完成时,您可以在需要的地方显示图像


对于gallery,您需要设置5个uri并显示它。

根据我的经验,您的问题没有简单的解决方案。使用并打算访问摄像头应用程序,您有两个与问题相关的选择:

  • 通过intent请求允许用户仅拍摄一张照片。在这种情况下,您可以提供存储照片的文件URI。若要使用此方法拍摄多张照片,应用程序必须多次启动intent。每次用户拍摄照片时,相机应用程序将退出并将用户返回到您的应用程序。This可能不是你想要的体验

  • 通过intent请求无限期地调用相机。在此模式下,用户可以
    long invokeTime = System.currentTimeMillis ();
    
    Uri IMAGES_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    getContentResolver().registerContentObserver (IMAGES_URI, true, this);
    
    @Override public void onChange (boolean selfChage)
    {
      onChange (selfChage, null);
    }
    
    @Override public void onChange (boolean selfChange, Uri uri)
    {
      listener.onContentChange (selfChange, uri);
    }
    
    String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED };
    String selection = null;
    String[] selectionArgs = null;
    order = MediaStore.Images.ImageColumns.DATE_ADDED + " DESC limit 1";
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            order);
    
    if (cursor.moveToNext())
    {
      int dateColumn = cursor.getColumnIndex (MediaStore.Images.Media.DATE_ADDED);
      int pathColumn = cursor.getColumnIndex (MediaStore.Images.Media.DATA);
      dateAddedMillis = cursor.getLong (dateColumn) * 1000;
      path = new File (cursor.getString (pathColumn));
    }
    cursor.close();
    
    create bitmap from image stored at "path";
    
    static private int ACTIVITY_REQUEST_CAMERA = 2;
    ...
    Intent intent = new Intent();
    intent.setAction (MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA););
    startActivityForResult (intent, ACTIVITY_REQUEST_CAMERA);