从应用程序复制图像时,只将URI复制到剪贴板上

时间:2018-05-23 13:03:29

标签: android

    imageview.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                //  android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //   clipboard.setText(CopyText);
                //   Toast.makeText(getApplicationContext(), "Text Copied to Clipboard", Toast.LENGTH_SHORT).show();

            } else {

                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.chandigarh);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                        b, "Title", null);
                Uri imageUri =  Uri.parse(path);
                ClipData.Item item = new ClipData.Item(imageUri);
                ClipData data = new ClipData("label", new String[]{"image/jpeg"}, item);
              //  ClipData theClip = ClipData.newUri(getContentResolver(), "Image", imageUri);
                clipboard.setPrimaryClip(data);
                Log.e("copy", String.valueOf(data));
                Toast.makeText(MainActivity.this, "copy to clipboard", Toast.LENGTH_SHORT).show();
                }

            //your stuff
            return true;
        }
    });

我想在长按时复制图像。以上是我的代码但我的问题是,当我只粘贴uri时显示。图像不会被复制。请告诉我怎么做?

0 个答案:

没有答案