0

Just getting into the Glide image loading library for Android. Currently working with some code from here: https://github.com/bumptech/glide/issues/459

My full project is here is you want to look at it:

https://github.com/mhurwicz/glide02

I'm getting the following exception when I run the app in the emulator in Android Studio:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

This is the key statement in MainActivity:

new ShareTask(this).execute("http://thelink");

(thelink is actually goo.gl/gEgYUd -- couldn't leave that in above because stackoverflow doesn't allow URL shorteners. )

Here is my code for the ShareTask class

class ShareTask extends AsyncTask<String, Void, File> {
    private final Context context;

    public ShareTask(Context context) {
        this.context = context;
    }
    @Override protected File doInBackground(String... params) {
        String url = params[0]; // should be easy to extend to share multiple images at once
        try {
            return Glide
                    .with(context)
                    .load(url)
                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get() // needs to be called on background thread
                    ;
        } catch (Exception ex) {
            Log.w("SHARE", "Sharing " + url + " failed", ex);
            return null;
        }
    }
    @Override protected void onPostExecute(File result) {
        if (result == null) { return; }
        Uri uri = FileProvider.getUriForFile(context, context.getPackageName(), result);
        share(uri); // startActivity probably needs UI thread
    }

    private void share(Uri result) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/jpeg");
        intent.putExtra(Intent.EXTRA_SUBJECT, "Shared image");
        intent.putExtra(Intent.EXTRA_TEXT, "Look what I found!");
        intent.putExtra(Intent.EXTRA_STREAM, result);
        context.startActivity(Intent.createChooser(intent, "Share image"));
    }
}

Using debug, it seems I may be running into trouble at the get() statement. For one thing, the width and the height are very large negative numbers. (See the code highlighted in green below.) Then the get() statement returns null. (See the code highlighted in red below.)

Thanks in advance for any help you can provide!

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
user1147171
  • 1,213
  • 3
  • 14
  • 22

1 Answers1

2

The NPE is coming from FileProvider.getUriForFile because you're passing in the wrong authority. You declared android:authorities="com.example.fileprovider" in the manifest, but you're using the package name at the call. This fails to resolve the info in FileProvider.parsePathStrategy. Match those two strings up and you'll be good to go.

The easiest fix is to use android:authorities="${applicationId}", this leads to 0 hardcoded strings, so you can keep using context.getPackageName().

Regarding your concerns during debug:

  • Target.SIZE_ORIGINAL is declared to be MIN_VALUE, hence the large number
  • it's not returning null, IDEA is just confused about where it is in the method, that return null; shouldn't be executed if it fails in the FileProvider code.
  • doGet(null): null is the timeout here, it's guarded properly in code

I've run the app and weirdly I got a log line saying

W/SHARE: Sharing http://... failed

but not a stack trace, which is weird, because ex cannot be null in a catch!

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • Thanks, @TWiStErRob - That did clear up that problem. However, I am now getting IllegalArgumentException: Failed to find configured root that contains /data/data/. Details here: http://stackoverflow.com/questions/41754799/android-illegalargumentexception-failed-to-find-configured-root-that-contains – user1147171 Jan 20 '17 at 01:50