2

I want to share an image with FileProvider, it works for Whatsapp, Dropbox, but facebook, google + etc crashes. Here is my sourcecode. I have simplified it a little: My manifest

My Fileprovider is empty:

public class MyFileProvider extends FileProvider {


}

My Sharingclass:

public class Sharing extends AsyncTask<String, String, Intent> {
Context cont;

public Sharing(Context cont) {
    this.cont = cont;

}

@Override
protected Intent doInBackground(String... arg0) {
    try {

        Bitmap b = BitmapFactory.decodeResource(cont.getResources(), R.drawable.ic_launcher);
        saveFile(cont, b);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/jpg");

        Uri uri = getImageUri(cont);
        List<ResolveInfo> resInfoList = cont.getPackageManager()
                .queryIntentActivities(intent,
                        PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {

            String packageName = resolveInfo.activityInfo.packageName;
            cont.grantUriPermission(packageName, uri,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        intent.putExtra(Intent.EXTRA_STREAM, uri);

        return intent;

    } catch (Exception e) {
        Log.e("shareerror", e.toString());
        e.printStackTrace();
        return null;
    }

}

public void saveFile(Context inContext, Bitmap inImage) throws Exception {
    File cacheDir = inContext.getCacheDir();
    File outFile = new File(cacheDir+"screenshot.jpg");

    FileOutputStream out = new FileOutputStream(outFile.getAbsolutePath());
    inImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.close();

}

public Uri getImageUri(Context incontext) {
    File cacheDir = incontext.getCacheDir();
    File urifile = new File(cacheDir, "screenshot.jpg");
    Uri photoUri = MyFileProvider.getUriForFile(incontext,
            "com.example.sharetest.MyFileProvider", urifile);

    return photoUri;
}
public void onPostExecute(Intent shareintent) {
    cont.startActivity(shareintent);
}

and file_path.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="photo_cache" path="/"/>

Does anybody see the mistake? Thank you :)

4

0 回答 0