मल्टी-विंडो मोड में, खींचें और छोड़ें

Android 7.0 (एपीआई लेवल 24) या इसके बाद के वर्शन वाले डिवाइस, मल्टी-विंडो (एक से ज़्यादा विंडो) की सुविधा पर काम करते हैं मोड जिसकी मदद से उपयोगकर्ताओं को खींचकर और छोड़कर डेटा को एक ऐप्लिकेशन से दूसरे ऐप्लिकेशन में ले जा सकते हैं.

जिस सोर्स ऐप्लिकेशन से कार्रवाई शुरू होती है उससे डेटा मिलता है. जहां ऑपरेशन खत्म होता है वहां टारगेट ऐप्लिकेशन को डेटा मिलता है.

जब उपयोगकर्ता कॉन्टेंट को खींचकर छोड़ना शुरू करता है, तो सोर्स ऐप्लिकेशन को DRAG_FLAG_GLOBAL इसे फ़्लैग करें यह बताता है कि उपयोगकर्ता, डेटा को किसी दूसरे ऐप्लिकेशन में खींच सकता है.

अलग-अलग ऐप्लिकेशन का डेटा, एक-दूसरे के साथ शेयर किया जाता है. इसलिए, ये ऐप्लिकेशन, डेटा का ऐक्सेस शेयर करते हैं कॉन्टेंट यूआरआई का इस्तेमाल करके. इसके लिए इन चीज़ों की ज़रूरत होती है:

  • सोर्स ऐप्लिकेशन को इनमें से कोई एक या दोनों सेट करना ज़रूरी है DRAG_FLAG_GLOBAL_URI_READ और DRAG_FLAG_GLOBAL_URI_WRITE फ़्लैग, जो स्रोत ऐप्लिकेशन के डेटा को पढ़ने या लिखने के ऐक्सेस पर निर्भर करते हैं टारगेट ऐप्लिकेशन को देना चाहता है.
  • टारगेट ऐप्लिकेशन को कॉल करना चाहिए requestDragAndDropPermissions() उपयोगकर्ता जिस डेटा को ऐप्लिकेशन में खींचता है उसे हैंडल करने से ठीक पहले. अगर आपने टारगेट किए गए ऐप्लिकेशन को, खींचकर छोड़ने वाले डेटा के ऐक्सेस की ज़रूरत नहीं होती. ऐप्लिकेशन ये काम कर सकता है: फिर कॉल करें release() चालू है वह ऑब्जेक्ट जो requestDragAndDropPermissions() से लौटाया गया. अगर ऐसा नहीं होता है, तो अनुमतियां तब रिलीज़ हो जाती हैं, जब शामिल गतिविधि नष्ट कर दिया गया. अगर लागू करने की प्रक्रिया में नई ऐक्टिविटी शुरू करना शामिल है, तो अगर यूआरआई हटाए गए हैं, तो आपको नई ऐक्टिविटी को वही अनुमतियां देनी होंगी. आपको क्लिप डेटा और फ़्लैग सेट करने होंगे:

    Kotlin

    intent.setClipData(clipData)
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    

    Java

    intent.setClipData(clipData);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

नीचे दिए गए कोड स्निपेट, यूज़र ड्रॉप कार्रवाई होने के तुरंत बाद खींचा गया डेटा. ज़्यादा जानकारी के लिए, 'खींचें और छोड़ें' डेमो देखें.

सोर्स गतिविधि

Kotlin

// Drag a file stored in an images/ directory in internal storage.
val internalImagesDir = File(context.filesDir, "images")
val imageFile = File(internalImagesDir, imageFilename)
val uri = FileProvider.getUriForFile(context, contentAuthority, imageFile)

val listener = OnDragStartListener@{ view: View, _: DragStartHelper ->
    val clipData = ClipData(ClipDescription("Image Description",
                                            arrayOf("image/*")),
                            ClipData.Item(uri))
    // Must include DRAG_FLAG_GLOBAL to permit dragging data between apps.
    // This example provides read-only access to the data.
    val flags = View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_GLOBAL_URI_READ
    return@OnDragStartListener view.startDragAndDrop(clipData,
                                                     View.DragShadowBuilder(view),
                                                     null,
                                                     flags)
}

// Container where the image originally appears in the source app.
val srcImageView = findViewById<ImageView>(R.id.imageView)

// Detect and start the drag event.
DragStartHelper(srcImageView, listener).apply {
    attach()
}

Java

// Drag a file stored in an images/ directory in internal storage.
File internalImagesDir = new File(context.getFilesDir(), "images");
File imageFile = new File(internalImagesDir, imageFilename);
final Uri uri = FileProvider.getUriForFile(context, contentAuthority, imageFile);

// Container where the image originally appears in the source app.
ImageView srcImageView = findViewById(R.id.imageView);

// Enable the view to detect and start the drag event.
new DragStartHelper(srcImageView, (view, helper) -> {
    ClipData clipData = new ClipData(new ClipDescription("Image Description",
                                                          new String[] {"image/*"}),
                                     new ClipData.Item(uri));
    // Must include DRAG_FLAG_GLOBAL to permit dragging data between apps.
    // This example provides read-only access to the data.
    int flags = View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ;
    return view.startDragAndDrop(clipData,
                                 new View.DragShadowBuilder(view),
                                 null,
                                 flags);
}).attach();

लक्ष्य गतिविधि

Kotlin

// Container where the image is to be dropped in the target app.
val targetImageView = findViewById<ImageView>(R.id.imageView)

targetImageView.setOnDragListener { view, event ->

    when (event.action) {

        ACTION_DROP -> {
            val imageItem: ClipData.Item = event.clipData.getItemAt(0)
            val uri = imageItem.uri

            // Request permission to access the image data being dragged into
            // the target activity's ImageView element.
            val dropPermissions = requestDragAndDropPermissions(event)
            (view as ImageView).setImageURI(uri)

            // Release the permission immediately afterward because it's no
            // longer needed.
            dropPermissions.release()
            return@setOnDragListener true
        }

        // Implement logic for other DragEvent cases here.

        // An unknown action type is received.
        else -> {
            Log.e("DragDrop Example", "Unknown action type received by View.OnDragListener.")
            return@setOnDragListener false
        }

    }
}

Java

// Container where the image is to be dropped in the target app.
ImageView targetImageView = findViewById(R.id.imageView);

targetImageView.setOnDragListener( (view, event) -> {

    switch (event.getAction()) {

        case ACTION_DROP:
            ClipData.Item imageItem = event.getClipData().getItemAt(0);
            Uri uri = imageItem.getUri();

            // Request permission to access the image data being dragged into
            // the target activity's ImageView element.
            DragAndDropPermissions dropPermissions =
                requestDragAndDropPermissions(event);

            ((ImageView)view).setImageURI(uri);

            // Release the permission immediately afterward because it's no
            // longer needed.
            dropPermissions.release();

            return true;

        // Implement logic for other DragEvent cases here.

        // An unknown action type was received.
        default:
            Log.e("DragDrop Example","Unknown action type received by View.OnDragListener.");
            break;
    }

    return false;
});