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

Added support for local phone stored images #70

Closed
wants to merge 4 commits into from
Closed

Added support for local phone stored images #70

wants to merge 4 commits into from

Conversation

rohansohonee
Copy link
Contributor

No description provided.

@ryanheise ryanheise self-requested a review June 26, 2019 11:13
Copy link
Owner

@ryanheise ryanheise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rohansohonee , thanks for the contribution and sorry for the delay in reviewing it.

There are a few changes I would like to request before merging it.

For performance reasons when there are many media items, the plugin currently loads artwork lazily when a media item actually becomes current, or alternatively there is an option shouldPreloadArtwork which which loads all artwork for all media items asynchronously in the background. Your patch for loading local files effectively loads all artwork for all media items synchronously regardless of the shouldPreloadArtwork option. I can understand this since loading from local files should be fast anyway and not a problem (maybe not if there are hundreds of media items), but if it is fast, then the same reasoning would mean that it would also not be a problem if you loaded artwork from local files in the same way that remote artwork is currently loaded, i.e. lazily on demand, unless the shouldPreloadArtwork option is enabled. I think this would also simplify your code, so even bitmaps loaded locally would go in the cache and you wouldn't need to make your change in createMediaMetadata, you could make it in loadArtBitmap.

Also, as your code is currently written, if multiple media items share the same artUrl, it will load the same local file multiple times creating multiple bitmaps in memory. If you put them in the cache, it would result in less memory usage.

Besides the above, I only had one other minor comment relating to coding standards. I haven't actually written up any coding standards for the project, but for Dart, I would follow Effective Dart and the /// comments as well as square brackets within them need to follow the Dart Doc spec so that they are picked up correctly by the dartdoc tool. For Java, the only change I would make is to insert a space in if(bitmap == null) between the if and the (.

@rohansohonee
Copy link
Contributor Author

rohansohonee commented Jun 26, 2019

Hi @ryanheise

Please check whether I have made the changes correctly. If not, please do let me know.

If what I have done is wrong can you please help out and explain.

Copy link
Owner

@ryanheise ryanheise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks on the right track. Although, you're still calling BitmapFactory.decodeFile before checking the cache, so it should be the other way around. Reverting to the original line of code in loadArtBitmap:

Bitmap bitmap = artBitmapCache.get(artUri.toString());

I would just insert your code immediately after this line, like this:

if (bitmap == null)
    bitmap = BitmapFactory.decodeFile(artUri);

Then you can delete the createBitmap method.

One more thing is you seem to have a duplicate import for BitmapFactory.

@rohansohonee
Copy link
Contributor Author

rohansohonee commented Jun 26, 2019

Hi @ryanheise

Thank you for replying so fast and helping me.
I have made the changes and also removed the duplicate import.

@ryanheise
Copy link
Owner

Looks good now, all of it amounts to just a 2 line change after simplifying. By the way, I'm curious (and I should have thought of this earlier), does it work if you use a file:// scheme without any code changes? i.e. Instead of:

/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1550234543481

Could you have used:

file:///storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1550234543481

@rohansohonee
Copy link
Contributor Author

Hi @ryanheise

The path I am getting for the device stored images is:

/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1550234543481

Also on android developer page it is mentioned that

public static Bitmap decodeFile (String pathName)

Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.

pathName: complete path name for the file to be decoded.

A valid path name should be given I guess for it to work is all.
The file:// scheme does not work in my case.

@ryanheise
Copy link
Owner

Yes, the file scheme will not work with decodeFile since that doesn't want a URL or a URI but if you revert to the original code (or just comment out the two additional lines you added) so that it uses the URL-based code in combination with decodeStream, I wondered if file:// would be a supported scheme of the URL class. i.e. If you convert your file path to a URI before passing it into artUri.

@rohansohonee
Copy link
Contributor Author

I am not sure on how to convert the file path to URI

@ryanheise
Copy link
Owner

In Dart, if path is your filepath, then:

    MediaItem mediaItem = MediaItem(
        id: 'audio_1',
        album: 'Sample Album',
        title: 'Sample Title',
        artist: 'Sample Artist',
        artUri: '${Uri.file(path)}');

@rohansohonee
Copy link
Contributor Author

Hi @ryanheise

It worked. No need for merging my changes. OMG sorry for all the trouble.
I should have seen how to convert to URI.
Really really sorry for all the trouble.

@ryanheise
Copy link
Owner

Cool, thanks for helping test it, and it's good to know file URIs are working (since I had never tested it myself).

@ryanheise ryanheise closed this Jun 26, 2019
@mrxten
Copy link

mrxten commented Dec 11, 2019

How get filePath from flutter asset or android resources?

@ryanheise
Copy link
Owner

Assets aren't files so they don't have a file path. However, you can copy a Flutter asset's data into a file with Dart code:

await file.writeAsBytes((await rootBundle.load(assetPath)).buffer.asUint8List());

If you search the standard Dart libraries, you can find more information on the asset APIs.

@mrxten
Copy link

mrxten commented Dec 11, 2019 via email

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants