i am implementing sharing in my app. it just has to be text share.all apps worked fine with sharing text with intent but facebook doesn't allow to share text via intent. so i implemented its sdk and wrote down this code.
ShareContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription(localThoughtDesc.get(finalI1))
.setContentUrl(Uri.parse("https://www.google.com"))
.build();
shareDialog.show(linkContent);
but i got output like this
i have gone through many tutorials. but most of them are deprecated. so if anyone can help me out it will be very nice.:)
Thank you :)
Facebook share doesn't support text. You can only share link and that will show the <meta content ="..."> text from that page with your link in Facebook.
Since i didn't get any way to post any thing on facebook so i tried another way to post text to facebook. Think it might helpful. By the way it is not actually sharing of text. I converted text into image file than i posted image through intent.
here's how i did it.
TextView textView=new TextView(getBaseContext());
textView.setTag("textView");
View view=innerLayout.findViewWithTag("textView");
String SCREENSHOTS_LOCATIONS = Environment.getExternalStorageDirectory().toString() + "/screenshots/";
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Bitmap.Config.ARGB_4444);
String path=SCREENSHOTS_LOCATIONS+ System.currentTimeMillis() + ".jpg";
final Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
System.out.println(bitmap.getHeight()+" "+bitmap.getWidth());
FileOutputStream fos = null;
try {
final File sddir = new File(SCREENSHOTS_LOCATIONS);
if (!sddir.exists()) {
sddir.mkdirs();
}
fos = new FileOutputStream(path);
System.out.println(sddir.getPath().toString());
if (fos != null) {
if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
Log.d("abc", "Compress/Write failed");
}
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent=new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TITLE, "Title");
intent.putExtra(Intent.EXTRA_SUBJECT, "Extra Subject");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(path))); //optional//use this when you want to send an image
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(intent, "send"), REQUEST_CODE);
Related
I just started using text for an app I am working on to further my android knowledge. However what I cannot figure out is how to get the pdf which was filled and then email it using the intent. I have been googling and researching everywhere but I cannot find anything. Does anyone know how to go about it?
declaration of my file before my onCreate;
File file = new File(getExternalFilesDir(null),"pvgform.pdf");
This is part of my code to add info to the fillable pdf text
OutputStream output = null;
try {
output = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
reader = new PdfReader(getResources().openRawResource(R.raw.form));
} catch (IOException e) {
e.printStackTrace();
}
try {
PdfStamper stamper = new PdfStamper(reader, output);
AcroFields acroFields = stamper.getAcroFields();
acroFields.setField("fullname", editText.getText().toString());
acroFields.setField("agedob", editText2.getText().toString());
acroFields.setField("description", editText3.getText().toString() + editText4.getText().toString() + editText5.getText().toString());
acroFields.setField("duration", editText6.getText().toString());
acroFields.setField("brandname", editText8.getText().toString());
acroFields.setField("genericname", editText9.getText().toString());
stamper.setFormFlattening(true);
stamper.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
reader.close();
and this is my attempt on the email intent for the pdf form:
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, "My form");
email.putExtra(Intent.EXTRA_TEXT, "Here is a form");
Log.d("file",file.getAbsolutePath());
Uri uri = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM,uri);
email.setType("application/pdf");
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
when I run this, there is no attachment to the email to be sent.
This is part of my code to add info to the fillable pdf text
You do not appear to be writing the PDF to a file.
and this is my attempt on the email intent for the pdf form:
Uri uri=Uri.parse("file://"+ "form.pdf");
This is not a valid Uri on any platform.
Write your PDF to a file, such as on external storage. Then, use a Uri that resolves to that file, using Uri.fromFile() to convert your File object into the appropriate Uri.
I use below code to take screen shot from my layout and share it via android intent but the captured screen shot in the selected app is not showing any thing.
#Override
public void onClick(View view) {
shareBitmap(this,takeScreenshot());
}
public Bitmap takeScreenshot() {
try{
View rootView = getWindow().getDecorView().findViewById(R.id.lyt_main_report_activity);
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
public static void shareBitmap(Context context, Bitmap bitmap){
//save to sd card
try {
File cachePath = new File(context.getCacheDir(), "images");
cachePath.mkdirs(); // don't forget to make the directory
FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
//start share activity
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = Uri.fromFile(newFile); //FileProvider.getUriForFile(context, "com.persianswitch.apmb.app.fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
// shareIntent.setDataAndType(contentUri, context. getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.setType("image/*");
context.startActivity(Intent.createChooser(shareIntent, context.getResources().getString(R.string.share_using)));
}
}catch (Exception ex){
ex.printStackTrace();
}
}
Please use this code this is tested code :
public static void takeScreenshot(Context context, View view) {
String path = Environment.getExternalStorageDirectory().toString() +
"/" + "test.png";
View v = view.findViewById(android.R.id.content).getRootView();
v.setDrawingCacheEnabled(true);
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
OutputStream out = null;
File imageFile = new File(path);
try {
out = new FileOutputStream(imageFile);
// choose JPEG format
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
} catch (FileNotFoundException e) {
// manage exception
} catch (IOException e) {
// manage exception
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception exc) {}
}
// onPauseVideo();
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
share.setType("image/png");
((Activity) context).startActivityForResult(
Intent.createChooser(share, "Share Drawing"), 111);
}
Since DrawingCache() deprecated above 28 so using Canvas will sort the issues for many. below answer can be used for share Screenshot including text without requesting for permissions.
To take the Screenshot
private Bitmap takeScreenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
To share the Screenshot
private void shareContent(Bitmap bitmap) {
String bitmapPath = MediaStore.Images.Media.insertImage(
binding.getRoot().getContext().getContentResolver(), bitmap, "title", "");
Uri uri = Uri.parse(bitmapPath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "App");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Currently a new version of KiKi app is available.");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
binding.getRoot().getContext().startActivity(Intent.createChooser(shareIntent, "Share"));
}
if nothing happens it means your Bitmap is null kindly check that. or you could also fall on View.buildDrawingCache(); which will draw the View to Bitmap and then call your View.getDrawingCache();
also When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers.
the quote was taken from the documented page
hope it helps
Tipp:The checked answer works when your device has external storage. On the Samsung 6 for example, it doesnt. Therefore you need to work with fileprovider.
Just incase somebody fell into this trap like me. The problem is that you are putting the name of the image twice.
FileOutputStream stream = new FileOutputStream(cachePath + "/image.png");
and than again.
File newFile = new File(imagePath, "image.png");
Change The seconed one to
File newFile = new File(imagePath);
Otherwise the contentUri give you the bitmap of your screenshot. I hope this helps someone. Took me 3 hours to figure it out :)
New to android. I'm taking a screen shot of my activity, and emailing it out. Here's my process:
public void emailScreenShot() {
//get bitmap of activity
View v = DeSlip.this.getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(),
v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
//save bitmap externally
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, AName + "vs" + BName+ ".JPEG");
Uri pngUri = Uri.fromFile(file);
FileOutputStream fos;
//compress
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("BoutSlip", e.getMessage(), e);
} catch (IOException e) {
Log.e("BoutSlip", e.getMessage(), e);
}
//email intent
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/png");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
startActivity(Intent.createChooser(emailIntent, "Send image using:"));
}
This works well, and gets my stuff out into the internet. My question is regarding using the external storage (I have permission in manifest). Is there some process I should be doing in order to remove or delete this bitmap after I have finished using it before I close? Or will the android memory management automatically take care of that with other stack items?
My gut says no, however you do want the bitmap around long enough to email (but I wasn't seeing it on the gallery app, so I know it's not available there).
I have written a simple application to view pictures. However, after sending the picture with the common intent:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("URLSTRING"));
shareIntent.setType("image/jpeg");
mActivity.startActivity(Intent.createChooser(shareIntent, "SHARE"));
The picture can be successfully sent if I chose Google hangout. But it is deleted after that!
Tested with other file manager application (root explorer) and it is the same behavior!
However, sent picture with GooglePlusGallery application does not seem to have this problem.
What is the trick? How to avoid the picture to be deleted?
I had the same problem with Hangouts. It seems that it deletes the file even if it doesn't succeed to send it, thus always copy the file to a new temp file before calling the intent.
I had the same issue, I believe exposing a ContentProvider instead that an uri could solve.
Same problem here. My app didn't even have storage write permissions, so I'm assuming Hangouts actually is deleting the images.
So instead of sharing the file directly, i make a copy first.
Here's the full solution:
Bitmap bm = BitmapFactory.decodeFile(filePath); // original image
File myImageToShare = saveToSD(bm); // this will make and return a copy
if (myImageToShare != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImageToShare));
shareIntent.setType("image/jpeg");
context.startActivity(Intent.createChooser(shareIntent, "Share using"));
}
private File saveToSD(Bitmap outputImage){
File storagePath = new File(Environment.getExternalStorageDirectory() + yourTempSharePath);
storagePath.mkdirs();
File myImage = new File(storagePath, "shared.jpg");
if(!myImage.exists()){
try {
myImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
myImage.delete();
try {
myImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream out = new FileOutputStream(myImage);
outputImage.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
return myImage;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
I am trying to share an image and text.
When I'm doing this:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screenshot.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/temporary_file.jpg"));
share.putExtra(android.content.Intent.EXTRA_TEXT, textToShare);
startActivityForResult(Intent.createChooser(share, "Share Image"),
FROM_SHARE);
It works greate on all apps (that was choosen from the popup dialog like Gmail) except for facebook, facebook takes only the image and not the text.
Is there a whay to pass an extra text with an image to the facebook app?
Thanks!
There is a bug in FACEBOOK I have the same problem If you really want to do that anyhow you need to use facebook SDK for that and use Facebook ShareDialog.