problem in taking screenshots in android by programmatically - android

in my app i am taking screenshot of the current screen and save it in sdcard. but in the case screenshot is not saved in sdcard. how to take screen shot and send the captured screen shot in email as attachment. please help me.
my coding:
View v1 = view.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
try
{
System.out.println("path "+Environment.getExternalStorageDirectory());
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+"/ff");
bm.compress(CompressFormat.PNG, 90, out);
}
catch (Exception e)
{
e.printStackTrace();
}
Intent emailIntent = new Intent(Intent.ACTION_SEND);
Uri U=Uri.parse("file:///sdcard/ff");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "aabc#gmail.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " from ..");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "from the app");
emailIntent.setType("image/png");
// emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,U);
emailIntent.putExtra(Intent.EXTRA_STREAM, U);
startActivity(Intent.createChooser(emailIntent, ""));
please help me.

i solved my problem by replace the try clause coding by
File file = new File(Environment.getExternalStorageDirectory()+"/filmfestival.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Related

Share image and text with Intent to Whats App

I am trying to share an image from imageview with text caption to whatsApp but the solutions I found online didn't seem to work for me.
View content = findViewById(R.id.posted_house);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try
{
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("image/*");
txtIntent .putExtra("message");
txtIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
Here is code for share image and text in whatsapp..
View screenView = rootView.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "Title", null);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
try {
startActivity(Intent.createChooser(intent, "Share Screenshot"));
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();
}

Not able to tweet image on twitter in Android

I'm trying to tweet an image on twitter from the SD card. Also tried to tweet from image URL, but the image not showing on composer.
Below is my code:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String path = destination.getPath();
Uri bmpUri = Uri.parse("file://" + destination.getAbsolutePath());
//Uri bmpUri = Uri.parse(path);
String body = "…Check it out for yourself !";
TweetComposer.Builder builder = new TweetComposer.Builder(this)
.text(body)
.image(bmpUri);
builder.show();
I have gone through available solutions but none of them helped. Please help me to know why an image is not tweeting.
Any help will be much appreciated.Thanks in advance.

Android Bitmap to File send with Intent

I have this code that gets an image from database as Bitmap and then writes this to a file and sends it with e-mail. This code works great.
I am trying to write this to a textfile that actually shows the picture.
Is this possible?
Do I need to write this to pdf file if I want a file that shows the image?
Here's my code
public void createBild(long x, String pathToFile, String fileName) {
Product product = dbHandler.findProductbyId(x);
Bitmap pic = BitmapFactory.decodeByteArray(dbHandler.fetchSingle(x), 0,
dbHandler.fetchSingle(x).length);
// create a file to write bitmap data
Bitmap bitmap = pic;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
File f = new File(pathToFile + "/"+fileName+".bmp");
try {
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Uri path = Uri.fromFile(f);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "test#live.se" });
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT, "body of email");
i.putExtra(Intent.EXTRA_STREAM, path);
try {
startActivity(Intent.createChooser(i, "Share"));
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(VisaData.this,
"There are no email clients installed.", Toast.LENGTH_SHORT)
.show();
}
}
Why don't you send it as a .png itself.
intent.setType("image/png");

save screen contents as a image into internal memory and share with social media programatically

Below code will capture the screen and store it in SD card. I want Then it will send this file via sharable apps.I want to store it in internal memory of phone instead but I am unable to do that.Please help me for the same.
WebView view = (WebView) findViewById(R.id.webView1);
#SuppressWarnings("deprecation")
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap( picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );
picture.draw( c );
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "score.png";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
if(fos!=null)
{
b.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
if(imagePath.exists())
{
sendMail(filePath);
}
else
{
Log.e("fie","file doesnt exist");
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
public void sendMail(String path) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
//emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
// new String[] { "youremail#website.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"My Score in Mock Test");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"PFA");
emailIntent.setType("image/png");
Uri myUri = Uri.parse("file://" + path);
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent, "share score card..."));
}
I tried to achieve this with below code found on stack overflow but it is not working. This code is not showing any exception when I debug through it but file is not creating to internal memory and so sending is failing.

Android save and share Image From SDCard

Hi I'm Trying to save a drawable as a bitmap to the SDCard and then share it using a share intent. but the problem i'm having is that its just not sharing the image does anyone know how to do this or where im going wrong?
heres what i have tried so far
getHelp.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
saveBitmapToExternalStorage(this, imageResourceFor Drawable, imageName);
File imageFile = new File("/sdcard/myfolder/"+ kitImagePath+".png");
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmap); //optional//use this when you want to send an image
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
}
});
public static void saveBitmapToExternalStorage(Context context, int imageResource, String imageName){
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(),imageResource);
//generate file
File dir = new File (Environment.getExternalStorageDirectory(), "/myfolder");
File f = new File(dir, String.format(imageName + ".png"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 , bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Instead of using Bitmap use its URI to share.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Subtitle");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ kitImagePath+".png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); //optional//use this when you want to send an image
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
Also as +Der Golem said. use Environment.getExternalStorageDirectory().getAbsolutePath() to get External storage path.
Add the following Permission in your manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Categories

Resources