মিডিয়া আর্টওয়ার্ক ডিসপ্লে করুন

মিডিয়া আইটেমের আর্টওয়ার্ক অবশ্যই লোকাল URI হিসেবে পাস করতে হবে, এর জন্য ContentResolver.SCHEME_CONTENT অথবা ContentResolver.SCHEME_ANDROID_RESOURCE ব্যবহার করতে হবে। এই লোকাল URI-কে অবশ্যই বিটম্যাপ বা ভেক্টর ড্রয়েবেলের যেকোনও একটিতে সমাধান করতে হবে।

আপনার অ্যাপের রিসোর্স থেকে আর্টওয়ার্ক প্রদান করুন

আপনার অ্যাপের রিসোর্স থেকে ড্রয়েবল প্রদান করতে, নিম্নলিখিত ফর্ম্যাটে URI পাস করুন:

android.resource://PACKAGE_NAME/RESOURCE_TYPE/RESOURCE_NAME

// Example URI - note that there is no file extension at the end of the URI
android.resource://com.example.app/drawable/example_drawable

এই স্নিপেট দেখায় যে কীভাবে রিসোর্স আইডি থেকে এই ফর্ম্যাটের URI তৈরি করতে হয়:

val resources = context.resources
val resourceId: Int = R.drawable.example_drawable

Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(resources.getResourcePackageName(resourceId))
    .appendPath(resources.getResourceTypeName(resourceId))
    .appendPath(resources.getResourceEntryName(resourceId))
    .build()

কন্টেন্ট প্রোভাইডার ব্যবহার করে আর্টওয়ার্ক প্রদান করা

এইসব ধাপ থেকে জানা যায় যে কীভাবে ওয়েব URI থেকে আর্ট ডাউনলোড করতে হয় এবং কন্টেন্ট প্রোভাইডার ব্যবহার করে লোকাল URI-এর মাধ্যমে তা প্রকাশ করতে হয়। সম্পূর্ণ উদাহরণের জন্য, Universal Android Music Player স্যাম্পেল অ্যাপে openFile-এর প্রয়োগ এবং আশেপাশের পদ্ধতিগুলি দেখুন।

  1. ওয়েব URI-এর সাথে মানানসই content:// URI তৈরি করুন। মিডিয়া ব্রাউজার পরিষেবা এবং মিডিয়া সেশন এই কন্টেন্ট URI-কে Android Auto এবং AAOS-এ পাস করে।

    Kotlin

    fun Uri.asAlbumArtContentURI(): Uri {
          return Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(CONTENT_PROVIDER_AUTHORITY)
            .appendPath(this.getPath()) // Make sure you trust the URI
            .build()
      }
    

    জাভা

    public static Uri asAlbumArtContentURI(Uri webUri) {
          return new Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(CONTENT_PROVIDER_AUTHORITY)
            .appendPath(webUri.getPath()) // Make sure you trust the URI!
            .build();
     }
    
  2. আপনার ContentProvider.openFile প্রয়োগ করার সময়, সংশ্লিষ্ট URI-এর জন্য কোনও ফাইল আছে কিনা তা চেক করুন। না হলে, ছবির ফাইল ডাউনলোড ও ক্যাশে করুন। এই কোড স্নিপেট Glide ব্যবহার করে।

    Kotlin

    override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
          val context = this.context ?: return null
          val file = File(context.cacheDir, uri.path)
          if (!file.exists()) {
            val remoteUri = Uri.Builder()
                .scheme("https")
                .authority("my-image-site")
                .appendPath(uri.path)
                .build()
            val cacheFile = Glide.with(context)
                .asFile()
                .load(remoteUri)
                .submit()
                .get(DOWNLOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS)
    
            cacheFile.renameTo(file)
             file = cacheFile
          }
          return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
     }
    

    জাভা

    @Nullable
      @Override
      public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode)
            throws FileNotFoundException {
          Context context = this.getContext();
          File file = new File(context.getCacheDir(), uri.getPath());
          if (!file.exists()) {
            Uri remoteUri = new Uri.Builder()
                .scheme("https")
                .authority("my-image-site")
                .appendPath(uri.getPath())
                .build();
            File cacheFile = Glide.with(context)
                .asFile()
                .load(remoteUri)
                .submit()
                .get(DOWNLOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    
            cacheFile.renameTo(file);
            file = cacheFile;
          }
          return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
      }