Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Is this the best way to download an image to share? #3399

Closed
jt-gilkeson opened this issue Nov 13, 2018 · 5 comments
Closed

[Question] Is this the best way to download an image to share? #3399

jt-gilkeson opened this issue Nov 13, 2018 · 5 comments
Labels

Comments

@jt-gilkeson
Copy link

jt-gilkeson commented Nov 13, 2018

Glide Version: 4.8.0

Integration libraries:OkHttp3 3.10.0

Issue details / Repro steps / Use case background:
I'm implementing a "Share photo" feature in my app, but I'm having trouble figuring out the "right" / most efficient way to do it with Glide v4. Basically if a user selects a scaled down "preview" image (view) and hits share, I want to give glide a URL, download the fullsize image contents, save it out to the cacheDir, and put it in a sharing intent. I've seen older examples (v3) with toBytes() and other things that seem more efficient than what I am doing, but I don't see how to do this with v4 - also SimpleTraget is warning me about being deprecated. Basically my question is what is the best way to do this and make sure I clean things up when I'm done with the bitmap / request?

override fun sharePostcard(url: String) {
   Glide.with(this).asBitmap().load(url).into(object: SimpleTarget<Bitmap>() {
      override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
        // Build the message
        val imageUri = getShareImageUri(this@MyActivity, resource)

        // Is this needed to clean things up?  (and is this the right way to do it)
        someView.post {
          Glide.with(this@MyActivity).clear(this)
        }

        // Code below included for completeness -getShareIntent to relevant to Glide usage
        val sharingIntent = ShareImageHelper.getShareIntent(subject, body, imageUri)

        startActivity(
          Intent.createChooser(
            sharingIntent,
            localizationManager.getLocalizedString(R.string.shareusing)
          )
       )
    }
  })
}  

/**
 * Creates a file and gets a Uri for sharing an image
 */
fun getShareImageUri(context: Context, image: Bitmap): Uri? {
    val path = File(context.cacheDir, IMAGE_SHARE_PREFIX + Date().time + JPG_EXTENSION)

    try {
        val fileOutputStream = FileOutputStream(path)
        image.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
        fileOutputStream.close()
    } catch (e: IOException) {
        return null
    }

    return getUriForFile(context, path)
}

/**
 * Gets a Uri for file
 */
 fun getUriForFile(context: Context, file: File): Uri {
    return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
        Uri.fromFile(file)
    } else {
        FileProvider.getUriForFile(context, FILE_PROVIDER, file)
    }
}
@stale
Copy link

stale bot commented Nov 20, 2018

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

@stale stale bot added the stale label Nov 20, 2018
@sjudd
Copy link
Collaborator

sjudd commented Nov 27, 2018

@stale stale bot removed the stale label Nov 27, 2018
@jt-gilkeson
Copy link
Author

Can't seem to get that to work in Kotlin any idea on what the syntax would be?

Also in onResourceReady is this the right way to clean things up / necessary:

someView.post {
    Glide.with(this@MyActivity).clear(this)
}

I want to basically do what @TWiStErRob is doing here using Glide v4 and Kotlin : #1424 (comment)

but can't for the life of me figure out if it's still possible and what the syntax would be.

@sjudd
Copy link
Collaborator

sjudd commented Dec 3, 2018

Not tested, but I think the v4 version of that issue comment is:

Glide.with(context)
  .as(byte[].class)
  .load(url)
  .apply(new RequestOptions()
    .override(MAX_IMAGE_SIZE, MAX_IMAGE_SIZE)
    .diskCacheStrategy(DiskCacheStrategy.DATA)
    .skipMemoryCache(true)
    .downsample(DownsampleStrategy.AT_MOST))
  .into(new SimpleTarget<byte[]>() {
        @Override public void onResourceReady(byte[] resource, GlideAnimation<? super byte[]> ignore 
 {
            new SaveAsFileTask().execute(resource);
        }
        @Override public void onLoadFailed(Exception ex, Drawable ignore) {
            toastUser("Whops, can't load " + link);
        }
        @Override public void onLoadCleared(Drawable ignored) {
          // Assert that SaveAsFileTask is cancelled and won't run, or already finished...
        }
    })

@stale
Copy link

stale bot commented Dec 10, 2018

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants