I am working on an app which requires specifically to take a screenshot of the screen and should be able to share it as a image file. I also need to include a hyperlink as caption to the image for Watsapp and Facebook. I am using the below code which share the image of the view. Need more advise on how to include link to the file.
XML File for Button to Create a image and Assign to ImageView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send_text"
android:id="@+id/send_text"
android:layout_below="@+id/brands"
android:onClick="sendMessage"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
Class Code
public void sendMessage(View view)
{
//Get a reference to EditText
EditText editText = (EditText)findViewById(R.id.message);
String msg = editText.getText().toString();
System.out.println(msg);
Intent intent = new Intent(this,BeerMessageActivity.class);
intent.putExtra(BeerMessageActivity.EXTRA_MESSAGE, msg);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
Bitmap b = snap(view.getRootView());
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
intent.putExtra(BeerMessageActivity.EXTRA_IMAGE, bs.toByteArray());
/*intent.putExtra(BeerMessageActivity.EXTRA_IMAGE,snap(view));*/
startActivity(intent);
}
public static Bitmap snap(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
Onclick Event on ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/message"
android:id="@+id/img_test"
android:onClick="shareit"/>
Code for shareit function
public void shareit(View view)
{
ImageView imageView = (ImageView)findViewById(R.id.img_test);//your layout id
Bitmap app_snap = ((BitmapDrawable)imageView.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, 100, 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);
startActivity(sharingIntent);
}
Please advise if conversion to byte array is required or we can do it without it. I have got to a place where the above code share a picture. via watsapp/facebook/Google+.