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"));
Related
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, "برنامه مورد نظر خود را انتخاب کنید"));
}
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);
I'm using the code below to share Image and text in Android. When I choose Whatsapp it shares the image and text together , but when I choose Telegram it just shares Image without any text! What's wrong in my code? Tnx
BitmapDrawable drawable = (BitmapDrawable) imageViewSample .getDrawable();
Bitmap bitmapImg = drawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(getContext() .getContentResolver(), bitmapImg, "Title", null);
Uri myUri= Uri.parse(path);
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM , myUri);
myBodyText="This is a test.";
share.putExtra(Intent.EXTRA_TEXT , myBodyText);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType("image/*");
startActivity(Intent.createChooser(share, "choose app"));
} catch (Exception e) {
e.printStackTrace();
}
Create a NEW File with your PATH passed as an argument, then use the method "fromFile(file name)" from the class Uri(Uniform Resource Identifier) and proceed with your code as usual.
BitmapDrawable drawable = (BitmapDrawable) imageViewSample .getDrawable();
Bitmap bitmapImg = drawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(getContext() .getContentResolver(),bitmapImg, "Title", null);
File myImage = new File(path); // introduce the new File
Uri myUri= Uri.fromFile(myImage); //Pass the file as argument
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM , myUri);
myBodyText="This is a test.";
share.putExtra(Intent.EXTRA_TEXT , myBodyText);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType("image/*");
startActivity(Intent.createChooser(share, "choose app"));
} catch (Exception e) {
e.printStackTrace();
}
It works. Look my code:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.setType("image/*");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.insta_image_j);
Bitmap bitmapImg = drawable.getBitmap();
sharingIntent.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmapImg, context));
sharingIntent.putExtra(
android.content.Intent.EXTRA_TEXT,
context.getString(R.string.share_text));
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(sharingIntent, context.getResources().getString(R.string.share_using)));
And I added these lines to Application's onCreate():
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
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"/>
I am using an activity to display a Full image and user has a button to save it.
Image is being loaded in imageview from URL where I have hosted image on internet.
I am saving an image as PNG file.
After saving image its quality is reduced. I know PNG is not lossless, but is there any way I can save an image as it is without any compromise with quality or resolution??
Here is the code:
buttonSave.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
fullSizeImage.buildDrawingCache();
Bitmap bm=fullSizeImage.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + File.separator + "MyWallpapers");
if(!myDir.exists())
{
myDir.mkdir();
}
String fname = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".png";
File file = new File (myDir, fname);
/*if (file.exists ()) file.delete (); */
try
{
//FileOutputStream out = new FileOutputStream(file);
/*FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);*/
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, bytes);
file.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
bytes.flush();
bytes.close();
/*out.flush();
out.close();*/
Toast.makeText(FullSizeImageDisplay.this, "Wallpaper Saved in SDcard/MyWallpapers folder",Toast.LENGTH_SHORT).show();
String[] paths = {root + File.separator + "MyWallpapers"};
String[] mediaType = {"image/png"};
MediaScannerConnection.scanFile(FullSizeImageDisplay.this, paths, mediaType, null);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
catch (Exception e)
{
e.printStackTrace();
}
OutputStream fOut = null;
Uri outputFileUri;
}
});
}
fullSizeImage.buildDrawingCache();
Bitmap bm=fullSizeImage.getDrawingCache();
Where fullSizeImage is may be an Image View.
I see problem is arising while you load the image. You must have used something like BitmapFactory.Options
Make following changes
BitmapFactory.decodeStream(<param1>,param2,null);