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

CB-14097: (android) Fix crash when selecting some files with getPicture #322

Merged
merged 2 commits into from
Jul 5, 2018

Conversation

bvmensvoort
Copy link
Contributor

Handles both urls:
content://com.android.providers.downloads.documents/document/1111
content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffilename.pdf

Platforms affected

Android

What does this PR do?

Fixes crash of certain URLs

What testing has been done on this change?

manual testing

Checklist

  • [ x ] Reported an issue in the JIRA database
  • [ x ] Commit message follows the format: "CB-3232: (android) Fix bug with resolving file paths", where CB-xxxx is the JIRA ID & "android" is the platform affected.
  • [ ? ] Added automated test coverage as appropriate for this change.

…re of urls with raw://

Handles both urls:
content://com.android.providers.downloads.documents/document/1111
content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffilename.pdf
@ochakov
Copy link
Contributor

ochakov commented Jul 1, 2018

Happened to me when selecting a photo from the Downloads folder.
Please merge this fix.
+1

Copy link
Member

@jcesarmobile jcesarmobile left a comment

Choose a reason for hiding this comment

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

Looks good, but can you do the two changes I requested?

Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

return getDataColumn(context, contentUri, null, null);
if (!TextUtils.isEmpty(id)) {
Copy link
Member

@jcesarmobile jcesarmobile Jul 1, 2018

Choose a reason for hiding this comment

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

Instead of using TextUtils just for checking if is empty, use id != null && id.length() > 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea to remove the dependency

} catch (NumberFormatException e) {
return null;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

if id is empty return null too

@bvmensvoort
Copy link
Contributor Author

I have implemented the suggestions of the review.
Must be better now.

@jcesarmobile jcesarmobile merged commit 5c23b65 into apache:master Jul 5, 2018
@jcesarmobile
Copy link
Member

Thanks, merged!

@jwburnside
Copy link

Is there a timeline for the next release? We're on 4.0.3 but this hasn't been merged in yet apparently.

@jcesarmobile
Copy link
Member

It’s merged, but not released

@bvmensvoort bvmensvoort deleted the Fix-for-CB-14097 branch September 28, 2018 11:02
@jordanorc
Copy link

Is there a timeline for the next release?

@osa-aq
Copy link

osa-aq commented Mar 31, 2019

This is very important, The camera is not working on a lot of android devices, When this will be released ?

@dusty-java-cup
Copy link

I need this fix. Please let me know how I can get around this problem. Is there a way I can manually apply this fix to my version of the plugin?

dusty-java-cup added a commit to dzchen0516/TrophiesTracker that referenced this pull request Apr 21, 2019
android). PROBLEM, the plugins/cordova-plugin-camera has a bug in it
that causes a problem with file paths
(apache/cordova-plugin-camera#322). I had to add
the fix manually then run the following commands:
ionic cordova platform rm android (OR IOS)
ionic cordova build android (OR IOS). Still need to implement the image
upload.
@rodrigograca31
Copy link

I'm also facing this issue.

We need this fix released!
There hasn't been a release in more than one year! We need it badly!

@claudiozam
Copy link

I'm also facing this issue.
Is there a timeline for the next release?
Thanks

@rodrigograca31
Copy link

@claudiozam probably no timeline.

I'm considering to use this repo directly to pull this fix to my code.....

@rodrigograca31
Copy link

Actually we just got a release!!!! @claudiozam

Thanks to @janpio ❤️ 🎉

@janpio
Copy link
Member

janpio commented Jun 29, 2019

Just a tag for now, release is in vote but will follow soon-ish. Watch for the rel/x.y.z tag that is made after it has been published to npm.

@rodrigograca31
Copy link

@janpio isnt a tag a release?
At least for me it is.... if I add the plugin it will add it like cordova-plugin-camera@4.1.0 ......

@janpio
Copy link
Member

janpio commented Jun 29, 2019

No, a tag is just a tag - but of course that can also be installed with the CLI. But doing cordova plugin add cordova-plugin-camera will only install tags that are also published to npm - and there it will take a few days until I can do that after a successful vote.

@rodrigograca31
Copy link

rodrigograca31 commented Jun 30, 2019

Maybe its because I used ionic but ionic cordova plugin add cordova-plugin-camera installs 4.1.0 directly from here

@rodrigograca31
Copy link

Oh wait! I was wrong!
Seems it installs ^4.0.3 .... I dont know why but I thought it was installing 4.1.0.

Can we see the vote status? Where?

Thanks!

@giuseeFG
Copy link

giuseeFG commented Nov 20, 2019

I solved it by editing the method "getRealPathFromURI_API11_And_Above" in this way:

@SuppressLint("NewApi")
    public static String getRealPathFromURI_API11_And_Above(final Context context, final Uri uri) {
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
           /* // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                if (id != null && id.length() > 0) {
                    if (id.startsWith("raw:")) {
                        return id.replaceFirst("raw:", "");
                    }
                    try {
                        final Uri contentUri = ContentUris.withAppendedId(
                                Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                        return getDataColumn(context, contentUri, null, null);
                    } catch (NumberFormatException e) {
                        return null;
                    }
                } else {
                    return null;
                }
            }*/
            // MediaProvider
            else if (isMediaDocument(uri) || isDownloadsDocument(uri)) {
                String docId = DocumentsContract.getDocumentId(uri);
                docId = docId.replace("msf:", "video:");
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{
                        split[1]
                };

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {

            // Return the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();

            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

@vo-va
Copy link

vo-va commented Jul 14, 2020

Hi @giuseeFG, your snippet helped me, thank you.
One question did you create pull request with it? If not I want to do it.

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

Successfully merging this pull request may close these issues.