0

I want to add a copy Button in RecyclerView items and copy the text, I added a code for copying the text from the RecyclerView but it is not working. After I clicked the copy Button, app crashes. This is my code to copy the text.

ClipboardManager clipboard = (ClipboardManager)holder.itemView.getContext().getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(Intent.EXTRA_TEXT, model.getShaData());
clipboard.setPrimaryClip(clip);

Toast.makeText(holder.itemView.getContext(), "Shayari Copied", Toast.LENGTH_SHORT).show();

MY LOGCAT

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: app.indian.desishayari, PID: 346
    java.lang.SecurityException: app.my.quotes from uid 10830 not allowed to perform READ_CLIPBOARD
        at android.os.Parcel.createException(Parcel.java:1966)
        at android.os.Parcel.readException(Parcel.java:1934)
        at android.os.Parcel.readException(Parcel.java:1884)
        at android.content.IClipboard$Stub$Proxy.setPrimaryClip(IClipboard.java:184)
        at android.content.ClipboardManager.setPrimaryClip(ClipboardManager.java:104)
        at app.indian.desishayari.ShayariAdapter$1.onClick(ShayariAdapter.java:50)
        at android.view.View.performClick(View.java:6740)
        at android.view.View.performClickInternal(View.java:6703)
        at android.view.View.access$3400(View.java:803)
        at android.view.View$PerformClick.run(View.java:26373)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:232)
        at android.app.ActivityThread.main(ActivityThread.java:7172)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
     Caused by: android.os.RemoteException: Remote stack trace:
        at android.app.AppOpsManager.noteOp(AppOpsManager.java:2411)
        at com.android.server.clipboard.ClipboardService.clipboardAccessAllowed(ClipboardService.java:653)
        at com.android.server.clipboard.ClipboardService.setPrimaryClipInternal(ClipboardService.java:488)
        at com.android.server.clipboard.ClipboardService.setPrimaryClipInternal(ClipboardService.java:423)
        at com.android.server.clipboard.ClipboardService$ClipboardImpl.setPrimaryClip(ClipboardService.java:267)

How to fix this issue? please anyone help...

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
Kishan Nath
  • 11
  • 1
  • 4

1 Answers1

0

Your copy code is wrong use this code i think work.I exampled when you click on item .

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("label", holder.yourTextView.getText().tostring());
            if (clipboard == null || clip == null) return;
            clipboard.setPrimaryClip(clip);
        }
    });
tohid noori
  • 221
  • 1
  • 11
  • thanks for responsive but this is also not works in my project but I found the solution... – Kishan Nath Jun 03 '20 at 14:58
  • `try { ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { ClipData data = ClipData.newPlainText("data", shayari.getGetShayari()); manager.setPrimaryClip(data); } else { manager.setText(shayari.getGetShayari()); } } catch (Exception e) { }` – Kishan Nath Jun 03 '20 at 14:59
  • oh yes you are checking the android version in the app. it seems that it doesn't work for below the honeyComb version – tohid noori Jun 03 '20 at 15:02