NullPointerException with Sharing Intent in Android Nougat - java

Currently, I'm trying to create a Sharing Intent to be called in the Android application itself when you click a "share" button to share an image. However, with the change to how Sharing Intents function in Android Nougat, I am getting a NullPointerException on this line:
Uri uImageFile = FileProvider.getUriForFile(DetailActivity.this, DetailActivity.this.getApplicationContext().getPackageName(), imageFile);
This was the error that I received:
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
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
Here's the full code for my Share Intent method:
private void shareIntent() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
File imageFile = new File(data.get(pos).getUrl());
Uri uImageFile = FileProvider.getUriForFile(DetailActivity.this, DetailActivity.this.getApplicationContext().getPackageName(), imageFile);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uImageFile);
startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.share_to)));
}
data.get(pos).getUrl() returns the URL of a custom class I made that implements Parcelable, and when printing it out, it returns a directory like the following:
"/storage/emulated/0/Pictures/primitive/Primitive-79538313.jpg"
Thank you for any help that you can provide!

Change your code to this
private void shareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jepg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+data.get(pos).getUrl()));
startActivity(Intent.createChooser(shareIntent, "Share image"));
}
Add this code in your activity onCreate()
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

Related

How to share image from recyclerView arraylist?

I am trying to share an images from arraylist,when I click on share button app list appears to share but when I select an app to share,it shows null in another app.
please help me to find out the solution :(
here is my recycler onBindViewHolder code.
holder.send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
final String image_path = fileslist.get(position).getUri().toString();
File share = new File(image_path);
Uri uri = Uri.fromFile(share);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(shareIntent, "Share to....."));
}
});
and here how my app is sharing

Image sharing to the other application by Intent

I am making an app and I wanna share images from my app to a specific app
so i used this code to share via choose
try {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// path of image
final String imageURIPath = "image-path";
Uri imageURI = Uri.parse(imageURIPath);
sharingIntent.setType("image/jpeg");
sharingIntent.setType("image/jpg");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageURI);
startActivity(Intent.createChooser(sharingIntent, "Share via app"));
} catch (Exception e) {
showMessage(e.toString());
}
and all work perfectly but I need to share to a specific app like this
shareIntent = getPackageManager().getLaunchIntentForPackage("app_PackageName");
startActivity(shareIntent);
so 'app_PackageName' chage to like app what i need like this example
shareIntent = getPackageManager().getLaunchIntentForPackage("com.adobe.lrmobile");
startActivity(shareIntent);
and I build the app without errors but when I wanna share to that app it just opens that app

startActivityForResult() is deprecated, use registerForActivityResult() [duplicate]

My app contains a simple Fragment that is used to open external web pages, with:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivityForResult(chooserIntent, RC_OPEN_URL);
And, when the result is returned (in my Fragment's onActivityResult(...)), the Fragment is popped off the backstack.
But, I'm now getting the new deprecation warning:
startActivityForResult(Intent,int) in Fragment has been deprecated
I've read the corresponding Getting a result from an activity documentation, but I'm not sure how to adapt the example they provide for my particular case.
I have discovered the ActivityResultContracts.StartActivityForResult class, but can't figure out how to pass my chooserIntent to it.
All the online examples for the class seem to be in Kotlin and I've not had any joy trying to decompile them to Java. So a Java example of how to use the new registerForActivityResult() method to open an external URL would be much appreciated.
There's no reason to use startActivityForResult() at all for createChooser() - you can use startActivity and run your code from onActivityResult() immediately after you call to startActivity:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivity(chooserIntent);
// Do your code from onActivityResult() here
However, if you really want to use the Activity Result API, then you can directly adapt the examples in the documentation, replacing the example GetContent contract with the StartActivityForResult contract:
// Create this as a variable in your Fragment class
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
new StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
// Do your code from onActivityResult
}
});
private void triggerChooser(Uri externalUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
mLauncher.launch(chooserIntent);
}
Below answer may help to someone.... But this is not readymade solution to above question.
I have faced lot of issue when I try to get result from activity to fragment.
Finally I found below solution.
In side the fragment, I created ActivityResultLauncher.
var myActivityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult<Intent, ActivityResult>(
ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback<ActivityResult> {
// ToDO:
if (it.resultCode == AppCompatActivity.RESULT_OK) {
}
}
) as ActivityResultLauncher<Intent>
And when I start the activity I used below code.
myActivityResultLauncher.launch(myIntent)

GIF is getting shared as an image not animated - Android Java

I am trying to share an GIF programmatically, but it is getting shared as an image (not animated)
Code Snippet:
Intent shareIntent = new Intent();
shareIntent.setType("image/gif");//image/gif
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(myList.get(item)));
startActivity(Intent.createChooser(shareIntent,"Share gif"));
You are trying to share to which apps?
Some whatsapp builds don't support gif feature.
You can check this question here with some additional information: How to share GIF image in Whatsapp programmatically in Android?
There is this comment:
"This solution doesn't work with whatsapp but does work with all the other apps I specified above (namely Hangouts, Android Messages, Facebook Messenger)"
He is referring to this answer:
private void ShareGif() {
// replace the path and file name with your gif image path and name
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "temporary_file.gif";
File sharingGifFile = new File(baseDir, fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/gif");
Uri uri = Uri.fromFile(sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share image"));
}
/*
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(shareIntent);
*/
Hope it helps.

how to start new activity for share action from CardView

I am trying to give the user the option to share cardview content.
i use this code in the main activity and it is working fine:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "some text to share";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
but when i use the same code from recyclerview i get error and the application crashed.
also i modified the code by adding FLAG and still not working
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = " teext to be shared";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(sharingIntent, "Share via"));
iam getting this error:
Calling startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
holder.iconFavorites_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// iam calling the code here and this block of code is located inside adapter class for recycleview .... the code located inside onBindViewHolder mithod
}
});
any body have solution for this problem ? if any information needed just tell me
In order to start an activity outside of another activity, you must use the FLAG_ACTIVITY_NEW_TASK flag for the intent (as you posted).
You would set this via sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Categories