Android Share Intent Image Sharing not working Except WhatsApp - java

I have checked the uri, the file saved is a bitmap and its returning file. But the image which shows during sharing is not valid. Gmail says it could not attach the attachment, messages app could not load the image.
Text sharing is working fine.
I am new to Android and is having problem sharing image through share intent. I have googled alot, tried various things but still could not find the solution.
public static void shareWallpaper(Context context, int wallpaperResourceId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), wallpaperResourceId);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/wallpaper" + String.valueOf(wallpaperResourceId) + ".jpg";
OutputStream out = null;
File file = new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getPath()));
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
context.startActivity(Intent.createChooser(shareIntent, "برنامه مورد نظر خود را انتخاب کنید"));
}

Related

How to make a clickable link when we share a link with image on a WhatsApp status from app android studio

Here is the code
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
File file = new File(this.getExternalCacheDir(), File.separator + "/" + "Wallpaper Bazaar" + ".jpeg");
BitmapDrawable bitmapDrawable = (BitmapDrawable) itemImage.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
try {
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_TEXT, "That's Awesome...\uD83D\uDC40 \n\n Install Now!☺☺ \n\n" + "https://play.google.com/store/apps/details?id=" + this.getPackageName());
startActivity(Intent.createChooser(intent, "Share Image from " + this.getString(R.string.app_name)));
} catch (Exception e) {
e.printStackTrace();
}
We are using this code to share the link with the image it's working correctly however when we share the link with the image on the WhatsApp status, the link work like a text, so our question is how to make a clickable link when we share a link with image on WhatsApp status. please help me to figure out the solution of this problem.

How to attach image to email on Android emailIntent

To send the email the method for the button is;
public void buttonSendEmailClicked(View view) {
File file = saveFileToShare();
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/image");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Check Out MyPic");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Taken With Android!");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), interstitial_request);
}
The saveFileToShare element is this;
public File saveFileToShare() {
try
{
File fileImage = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/attachment.png");
if(!fileImage.exists())
{
fileImage.delete();
}
editorImage.setDrawingCacheEnabled(true);
Bitmap bitmap = editorImage.getDrawingCache();
fileImage.createNewFile();
FileOutputStream ostream = new FileOutputStream(fileImage);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
editorImage.invalidate();
editorImage.setDrawingCacheEnabled(false);
return fileImage;
}
catch (Exception e)
{
System.out.print(e);
e.printStackTrace();
return null;
}
}
Saving the image works fine, the save code is;
public void buttonSaveImageClicked(View view) throws IOException {
editorImage.setDrawingCacheEnabled(true);
Bitmap bitmap = editorImage.getDrawingCache();
SaveLayoutToFile saveImage = new SaveLayoutToFile(this, bitmap, editorImage);
String filePath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/wonkydog";
saveImage.execute(filePath);
}
I need to set the email code to grab the image and attach to email.
At the moment when I press the email button it just returns to the title screen without doing anything else.
If I comment out this line
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
Then it opens the mail send dialogue, but without attachment of course...
I found the answer, rather than using the emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
I replaced it with;
emailIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(EditorActivity.this, "com.myapp.myappname.provider", file));
It now works correctly!

How to share Screenshot of current page by using intent?

I am developing and android blog application where I want to share my current blog page screenshot.I try but it showing file format is not supported..please help me to find my error..Thanks in advance
MyAdapter.java
sendImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bitmap app_snap = ((BitmapDrawable)movie_image.getDrawable()).getBitmap();
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SaveImg";
System.out.println("****FILEPATH **** : " + file_path);
File imagePath = new File(Environment.getExternalStorageDirectory() + "/scr.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
System.out.println("****FILEPATH1 **** : " + file_path);
app_snap.compress(Bitmap.CompressFormat.PNG, 200, fos);
System.out.println("****FILEPATH2 **** : " + file_path);
fos.flush();
fos.close();
}
catch (IOException e) {
System.out.println("GREC****** "+ e.getMessage());
}
Intent sharingIntent = new Intent();
Uri imageUri = Uri.parse(imagePath.getAbsolutePath());
sharingIntent.setAction(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
context.startActivity(sharingIntent);
}
});
Here is the code that allowed my screenshot to be stored on an SD card and used later for whatever your needs are:
First, you need to add a proper permission to save the file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
And this is the code (running in an Activity):
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
And this is how you can open the recently generated image:
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
If you want to use this on fragment view then use:
View v1 = getActivity().getWindow().getDecorView().getRootView();
instead of
View v1 = getWindow().getDecorView().getRootView();
on takeScreenshot() function
Note:
This solution doesn't work if your dialog contains a surface view. For details please check the answer to the following question:
Android Take Screenshot of Surface View Shows Black Screen
if u have any doubt please go through this link
You can get the Bitmap of your screen view inside your layouts. Where view will be your layout views like Linear or RelativeLayout
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap returnedBitmap = view.getDrawingCache();
then have to convert into byte[] so that you can send with the Intent like this following:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
returnedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
and send data with Intent like this:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bitmap_",byteArray);
get Intent on your SecondActivity like this:
byte[] byteArray = getIntent().getByteArrayExtra("bitmap_");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

For example android-screenshot-library

ASL (android-screenshot-library) Have a working example?
OR
How do you show an example used (How to use)?
(sorry for my English)
private void getScreenShot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
To open the captured snap.
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
You need
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Image doesn't add to Gallery after saving

I'm using couple of commands from this answer to save my bitmap on SD card and then share it via intent.
and here is my final code:
View u = findViewById(R.id.mainL);
u.setDrawingCacheEnabled(true);
LinearLayout z = (LinearLayout) findViewById(R.id.mainL);
int totalHeight = z.getHeight();
int totalWidth = z.getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "pics/screenshot.jpeg";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
b.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
Uri bmpUri = Uri.parse(filePath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(shareIntent, "Share"));
but now I have two problems.
1)the result Image (screenshot.png) is not reachable with mobile gallery (there is a image file in pics folder in sd card although).
2)when I try to share it via intent, it doesn't send and for example when I send it via Bluetooth the receiver gadget breaks the sending operation.
thanks.
ohk just paste this line after adding any pic in the gallery it will refresh your gallery
It worked for me hope will help u :)
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
try this
//save image into sdcard
FrameLayout f=(FrameLayout)findViewById(R.id.framelayout);
f.setDrawingCacheEnabled(true);
Bitmap bm = f.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte1 = stream.toByteArray();
String path = Environment.getExternalStorageDirectory().toString();
File imgDirectory = new File(Environment.getExternalStorageDirectory()+"/images/");
imgDirectory.mkdirs();
OutputStream fOut = null;
File file = null;
file = new File(path,"/images/"+etcardname.getText().toString()+ ".png");
Toast.makeText(getBaseContext(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
//share image
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
startActivity(Intent.createChooser(share, "Share image using"));

Categories

Resources