如何将旋转图像发送到第二个活动

时间:2016-02-25 04:47:44

标签: android database image rotation android-recyclerview

我正在将图库中的所有图像显示到循环视图中并实现代码以旋转它但我不知道如何将此旋转图像发送到下一个活动。

请帮我解决这个问题。我是android的新手

public class UploadQueue extends Activity {

    public QueueAdapter queueAdapter;
    public ArrayList<QueueItem> queueItems = new ArrayList<QueueItem>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uploadqueue);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);

        final ImageButton btnSelect= (ImageButton) findViewById(R.id.select);
        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ArrayList<QueueItem> selected = queueAdapter.getSelected();
                String[] allPath = new String[selected.size()];

                for (int i = 0; i < allPath.length; i++) {
                    allPath[i] = selected.get(i).path;
                }

                if (allPath.length <=10)
                {

                    Intent intent=new Intent(UploadQueue.this,ImageGalleryActivity.class);
                    intent.putExtra("all_path", allPath);
                    startActivity(intent);

                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Maximum 10 images share",Toast.LENGTH_LONG).show();
                }

            }

        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        queueAdapter = new QueueAdapter(UploadQueue.this, queueItems);
        recyclerView.setAdapter(queueAdapter);
        Intent i= getIntent();
        String[] all_path = i.getStringArrayExtra("all_path");
        queueAdapter.initialize(all_path);


    }


    public boolean onKeyDown(int keycode, KeyEvent event) {
        if (keycode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
        }
        return super.onKeyDown(keycode, event);
    }


    public class QueueAdapter extends RecyclerView.Adapter<ViewHolder> {

        public ArrayList<QueueItem> queueItems;
        public Context context;

        public QueueAdapter(UploadQueue context, ArrayList<QueueItem> queueItems) {
            this.queueItems=queueItems;
            this.context=context;
        }

        public void delete(int position) {

            queueItems.remove(position);
            Log.d("Deny", "" + position);
            notifyItemRemoved(position);
            Log.d("Deny", "" + position);


        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View itemLayoutView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.uploadqueueitem, null);

            ViewHolder viewHolder = new ViewHolder(itemLayoutView);
            return viewHolder;

        }

        public ArrayList<QueueItem> getSelected() {
            ArrayList<QueueItem> dataT = new ArrayList<QueueItem>();

            for (int i = 0; i < queueItems.size(); i++) {

                    dataT.add(queueItems.get(i));

            }


            return dataT;
        }


        @Override
        public void onBindViewHolder(ViewHolder viewHolder, final int position) {

            QueueItem item = getItem(position);


            Bitmap bm = BitmapFactory.decodeFile(item.path);
            viewHolder.imageview.setImageBitmap(bm);

        }

        public long getItemId(int position) {
            return position;
        }

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

        public QueueItem getItem(int position) {
            return queueItems.get(position);
        }

        public void initialize(String[] all_path) {

            queueItems.clear();
            for (String item : all_path) {
              QueueItem queueItem = new QueueItem();
                queueItem.path =item;
                Log.e("UploadQueue Path ", queueItem.path);

                queueItems.add(queueItem);
            }
            Log.e("QueueItem SIze()", "" + queueItems.size());
            notifyDataSetChanged();
            return;
        }

    }


    class QueueItem
    {
        String path;
    }

    class ViewHolder extends RecyclerView.ViewHolder  {

        public ImageView imageview;
        ImageButton btnLeft,btnRight,btnDelete;

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);

            imageview = (ImageView) itemLayoutView.findViewById(R.id.QueueItemThumbnail);
            btnDelete= (ImageButton) itemLayoutView.findViewById(R.id.delete);
            btnLeft= (ImageButton) itemLayoutView.findViewById(R.id.rotateLeft);
            btnRight= (ImageButton) itemLayoutView.findViewById(R.id.rotateRight);

            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    queueAdapter.delete(getAdapterPosition());
                }

            });

            btnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Log.e("POSITIN LEFT ", "" + getAdapterPosition());
                     imageview.setRotation(imageview.getRotation() + 90);


                }
            });

           btnRight.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                   Log.e("POSITIN RIGHT  ", "" + getAdapterPosition());
                    imageview.setRotation(imageview.getRotation() - 90);


                }
            });
        }

    }

ImageGalleryActivity类

public class ImageGalleryActivity extends Activity {


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

        Intent i= getIntent();
        String[] all_path = i.getStringArrayExtra("all_path");
        addImagesToThegallery(all_path);
    }

    private void addImagesToThegallery(String[] all_path) {

        LinearLayout imageGallery = (LinearLayout)findViewById(R.id.imageGallery);
        for(String image : all_path) {
            imageGallery.addView(getImageView(image));
        }

    }

    private View getImageView(String image) {

        ImageView imageView = new ImageView(getApplicationContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 0, 10, 0);
        imageView.setLayoutParams(lp);
        //set images
        Bitmap bm = BitmapFactory.decodeFile(image);
        imageView.setImageBitmap(bm);

        return imageView;
    }

}

提前致谢

1 个答案:

答案 0 :(得分:0)

通过putExtra传递位图

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
 startActivity(intent);

并在另一端检索它:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
imageView.setImageBitmap(bitmap);