在Android中,在水平滚动视图中显示相机点击图像并动态显示视图。当你开始添加到视图中时

时间:2016-12-16 12:28:15

标签: image android-viewpager android-recyclerview horizontal-scrolling

在Android应用程序中...单击图像从相机使用按钮单击并在活动中的小区域动态显示所有单击的图像到水平滚动视图,而不是像画廊视图那样的整个活动,但不想在特定图像上使用图库和onlongpressed显示一个删除它的对话框。

1 个答案:

答案 0 :(得分:1)

我已经开发出了解决方案..

        MainActivity.java
     package com.example.mtk.recyclerviewdemo;

    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Environment;
    import android.provider.ContactsContract;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Locale;

    import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE;
    import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
    import static android.support.v4.app.ActivityCompat.startActivityForResult;

    public class MainActivity extends AppCompatActivity {

        static final int REQUEST_TAKE_PHOTO = 1;
        private int SELECT_FILE = 2;
        private static Button btnclick;
        private String mCurrentPhotoPath;
        private static RecyclerView recyclerView;
        ArrayList<Data_Model> arrayList;
        String chkpath = null;
        MyAdapter m;
        int i = 0;
        private String userChoosenTask;
        private Uri fileUri; // file url to store image/video

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

            btnclick = (Button) findViewById(R.id.click);
            recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

            arrayList = new ArrayList<>();
            m = new MyAdapter();
            Log.d("oncreate", "set adapter");
            recyclerView.setAdapter(m);
            ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
            params.height = 200;
            recyclerView.setLayoutParams(params);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
            recyclerView.setLayoutManager(linearLayoutManager);


            //Camera Click image
            btnclick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectImage();

                }
            });


        }

        public void takepicture() {
            Log.d("Cameraclick", "takepicture");

            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }

        private File createImageFile() throws IOException {
            // Create an image file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );

            // Save a file: path for use with ACTION_VIEW intents
            mCurrentPhotoPath = image.getAbsolutePath();
            return image;


        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

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

                    // Save Image To Gallery
                    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    File f = new File(mCurrentPhotoPath);
                    Uri contentUri = Uri.fromFile(f);
                    mediaScanIntent.setData(contentUri);
                    this.sendBroadcast(mediaScanIntent);
                    String clickpath = mCurrentPhotoPath;
                    Data_Model data_model = new Data_Model();
                    data_model.setImage(clickpath);
                    arrayList.add(data_model);
                    m.notifyDataSetChanged();


                } else if (requestCode == SELECT_FILE) {
                    Log.d("gallery", "checkpointd");
                    onSelectFromGalleryResult(data);
                }
            }

        }


        class MyAdapter extends RecyclerView.Adapter<MyAdapter.Myviewholder> {

            @Override
            public Myviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = getLayoutInflater().inflate(R.layout.item_rowchk, parent, false);
                Myviewholder myviewholder = new Myviewholder(v);
                Log.d("myactivty ", "oncreateViewHolder");

                return myviewholder;
            }

            @Override
            public void onBindViewHolder(Myviewholder holder, final int position) {
                final Data_Model m = arrayList.get(position);
                Log.d(" myactivty", "onBindviewholder" + position);
                //holder.imageView.setVisibility(View.VISIBLE);
                // bimatp factory
                BitmapFactory.Options options = new BitmapFactory.Options();
                // downsizing image as it throws OutOfMemory Exception for larger
                // images
                options.inSampleSize = 20;
                final Bitmap bitmap = BitmapFactory.decodeFile(m.getImage(), options);
                holder.imageView.setImageBitmap(bitmap);
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getApplicationContext(), ImageDisplay.class);
                        String chkpath = m.getImage();
                        intent.putExtra("path", chkpath);
                        Log.d("intent", "new activity");
                        startActivity(intent);
                    }
                });
                holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {

                        final int pst = position;
                        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case DialogInterface.BUTTON_POSITIVE:
                                        arrayList.remove(position);
                                        notifyItemRemoved(position);
                                        notifyItemRangeChanged(position, arrayList.size());
                                        // Yes button clicked
                                        Toast.makeText(MainActivity.this, "Yes Clicked",
                                                Toast.LENGTH_LONG).show();
                                        break;

                                    case DialogInterface.BUTTON_NEGATIVE:
                                        // No button clicked
                                        // do nothing
                                        Toast.makeText(MainActivity.this, "No Clicked",
                                                Toast.LENGTH_LONG).show();
                                        break;
                                }
                            }
                        };

                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("Are you sure want to Delete ?")
                                .setPositiveButton("Yes", dialogClickListener)
                                .setNegativeButton("No", dialogClickListener).show();

                        return true;
                    }
                });


            }

            @Override
            public int getItemCount() {
                return arrayList.size();
            }

            public class Myviewholder extends RecyclerView.ViewHolder {
                public ImageView imageView;

                public Myviewholder(View itemView) {
                    super(itemView);
                    imageView = (ImageView) itemView.findViewById(R.id.image);
                }
            }
        }

        private void onSelectFromGalleryResult(Intent data) {

            Uri selectedImageUri = data.getData();
            String[] projection = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String path = cursor.getString(column_index);
            Data_Model data_model = new Data_Model();
            data_model.setImage(path);
            arrayList.add(data_model);
            m.notifyDataSetChanged();
            // Toast.makeText(this, "" + path, Toast.LENGTH_LONG).show();
        }

        private void selectImage() {

            final CharSequence[] items = {"Take Photo", "Choose from Library",
                    "Cancel"};

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Add Photo!");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    boolean result = Utility.checkPermission(MainActivity.this);

                    if (items[item].equals("Take Photo")) {
                        userChoosenTask = "Take Photo";
                        if (result)
                            takepicture();

                    } else if (items[item].equals("Choose from Library")) {
                        userChoosenTask = "Choose from Library";
                        if (result)
                            galleryIntent();


                    } else if (items[item].equals("Cancel")) {
                        dialog.dismiss();
                    }
                }
            });
            builder.show();
        }


        public void galleryIntent() {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            Log.d("gallery", "checkpointA");
            startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
            Log.d("gallery", "checkpointB");
        }
}

        Data_Model.Java
    package com.example.mtk.recyclerviewdemo;

    import android.net.Uri;


    public class Data_Model {
        private String image;


        // Getter and Setter model for recycler view items

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }


    }
        ImageDisplay.java

    package com.example.mtk.recyclerviewdemo;

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ImageView;

    public class ImageDisplay extends AppCompatActivity {
     ImageView imageView;
        private  String path;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_image_display);
            imageView = (ImageView) findViewById(R.id.imageviewnew);
            //setupActionBar();

            // Get Image Path
            path = getIntent().getExtras().getString("path");

            // Get Image
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions. inJustDecodeBounds = false;
            bmOptions. inSampleSize = 4;
            bmOptions. inPurgeable = true ;
            Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);

            // Display Image
            imageView.setImageBitmap(bitmap);
        }
    }



     xml.files

    activity_image_display.xml
    activity_main.xml
    item_row.xml
    item_rowchk.xml

    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:orientation="vertical"
        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.mtk.recyclerviewdemo.MainActivity"
        android:weightSum="1">

        <Button
            android:id="@+id/click"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="CLICK"
            android:background="#fabdca"
            android:textSize="27sp" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="20dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BUTTON CHECK"/>


    </LinearLayout>

item_rowchk.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:rotation="90"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Imagedisplay.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_image_display"
    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.mtk.recyclerviewdemo.ImageDisplay">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rotation="90"
    android:id="@+id/imageviewnew" />

</LinearLayout>