How to share Screenshot of current page by using intent? - java

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);

Related

Android Share Intent Image Sharing not working Except WhatsApp

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, "برنامه مورد نظر خود را انتخاب کنید"));
}

How can I share images from an Array that uses ImageView feature?

I have a doubt.
How can I share images from an Array that uses ImageView feature?
My array has more than 100 images, an example:
Final int [] photos = {
R.drawable.abrir_a_boca,
R.drawable.rooms,
R.drawable.firmly,
R.drawable.agradeca,
R.drawable.alfaiate,
R.drawable.ancora,
}
To share I'm trying to use Intent.ACTION_SEND
Set.setOnClickListener (new View.OnClickListener () {
#Override
Public void onClick (View v)
{
Intent sharingIntent = new Intent (Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse (photos???);
SharingIntent.setType ("image / *");
SharingIntent.putExtra (Intent.EXTRA_STREAM, screenshotUri);
StartActivity (Intent.createChooser (sharingIntent, "Share image using"));
}
});
How can I share the images?
Thank you so much!!!
You have to do some operations before you share your image, so add the permission in your Manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Fist, create a Bitmap object from your drawable resource:
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx);
Then, get the path for you share your image
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/yourImage.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();
}
path=file.getPath();
Uri uri = Uri.parse("file://"+path);
And finally, create your intent:
Intent intent = new Intent();
intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/jpg");
startActivity(Intent.createChooser(intent,"Share with..."));

sharing images from drawable

i'm trying to send images from my app, but it only sends one image (the one mentioned in the code below image_intro.
i want the app to share whatever image the user chooses.
Here is the code i used:
// Share event start
final Button share = (Button) findViewById(R.id.share);
share.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.image_intro);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.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();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
Intent shareIntent = new Intent();
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/jpg");
startActivity(Intent.createChooser(shareIntent,"Share with"));
}
});
im counting in your help friends, thanks
i managed to solve it by adding these two lines:
ImageView image = (ImageView) findViewById(R.id.ba‌​ckgroundPreview);
Bitmap bitmap = ((BitmapDrawable)ima‌​ge.getDrawable()).get‌​Bitmap();

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