I have Android application which interacts with Web Server. When data is sent from phone to server, there is a bit of work with data (10-20 secs) and after that I get result back to phone. And i display that result in dialog with 2 choices. One choice is "OK" and it means that user is fine with result and dismiss the dialog. The other choice makes a screenshot of that results. And when I press this button I don't get picture of dialog, i get picture of layout which is behind that dialog.
My code behind this is
//this method is called on onPostExecute of async task
public void recieveResult (final Activity act,String result){
new AlertDialog.Builder(act)
.setTitle("Data - status")
.setMessage(result)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("Save picture", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Bitmap bitmap = takeScreenshot(act);
saveBitmap(bitmap,act);
Toast.makeText(act,"Saved",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
})
.show();
}
public Bitmap takeScreenshot(Activity act) {
View rootView = act.findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap,Activity act) {
String timeStamp = new SimpleDateFormat("MMyyyydd_HHmmss").format(new Date());
String imageFileName = "/screenshot_"+timeStamp+"_.png";
File storage = Environment.getExternalStoragePublicDirectory("Screens");
if (!storage.exists())
{
Toast.makeText(act, "Directory made", Toast.LENGTH_LONG).show();
storage.mkdirs();
}
File imagePath = new File(storage.getPath() + imageFileName);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
My result of picture is just screen without dialog. How to get picture of dialog in it?
Try obtaining the Windows's decor view
then get its drawing cache bitmap as you implemented.
Dialog has it's own window ( different form activity's window ), you can use dialog.getWindow() method to get dialog's window.
so here is the screenshot code:
private AlertDialog dialog;
public void showDialog(View view) {
dialog = new AlertDialog.Builder(this)
.setTitle("Titel")
.setMessage("some content")
.setPositiveButton("Screenshot", (d, which) -> {
Bitmap bitmap = screenshot(dialog);
// you can save the bitmap or display in an ImageView
}).create();
dialog.show();
}
private Bitmap screenshot(Dialog dialog) {
Window window = dialog.getWindow();
View decorView = window.getDecorView();
Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
decorView.draw(new Canvas(bitmap));
return bitmap;
}
try this code
String picId=String.valueOf(nu);
String myfile="meter"+picId+".jpeg";
BitmapDrawable bitmapDrawable = null;
Toast.makeText(getActivity(),"success full store image gallery",Toast.LENGTH_SHORT).show();
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
File dir_image = new File(Environment.getExternalStorageDirectory()+//<---
File.separator+"SoundMeter"); //23-1-16 //<---
dir_image.mkdirs();
// 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 = surfaceView.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
/* bitmapDrawable = new BitmapDrawable(bitmap);*/
v1.setDrawingCacheEnabled(false);
// File imageFile = new File(mPath); //file path 23-1-16
File imageFile = new File(dir_image,myfile);
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();
}
btnCaptured1.setVisibility(View.VISIBLE);
btnCaptured.setVisibility(View.VISIBLE);
//btnCaptured.setBackgroundDrawable(bitmapDrawable);
}
});
}
else
{
Toast.makeText(getActivity().getApplicationContext(), "No Connection", Toast.LENGTH_LONG).show();
}
Related
I'm building an app with 2 fragments.
1st Fragment: ImageView and 2 TextViews
2nd Fragment: 2 EditTexts and 3 Buttons
In the 2nd Fragment, I have a "Save" Button. In the onClick of this button, I am trying to send a local broadcast:
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
localBroadcastManager.sendBroadcast(localIntent);
}
});
And this is my intent:
final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager
.getInstance(Objects.requireNonNull(getContext()));
final Intent localIntent = new Intent("CUSTOM_ACTION");
The point of this is so that the 1st fragment picks it up and executes a method to save the Image as a bitmap with its captions (that's what the 2 TextViews are for). This is my attempt:
private BroadcastReceiver listener = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent ) {
String data = intent.getStringExtra("DATA");
Toast.makeText(context, data + " received", Toast.LENGTH_SHORT).show();
createAndSaveBitmap(topTextView.getText().toString(), bottomTextView.getText().toString(), memeBitmap, memeCanvas, memePaint, imageView, imageUri);
}
};
public void createAndSaveBitmap(String top, String bottom, Bitmap bitmap,
Canvas canvas, Paint paint, ImageView imageView, Uri imageUri) {
try {
memeBitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageUri))
.copy(Bitmap.Config.ARGB_8888, true); }
catch (FileNotFoundException e) { e.printStackTrace(); }
canvas = new Canvas(memeBitmap);
paint = new Paint();
canvas.drawText(top, 0, 0, paint);
canvas.drawText(bottom, 0, memeCanvas.getHeight() - 10, paint);
imageView.setImageBitmap(memeBitmap);
if(bitmap != null){
File file = Environment.getExternalStorageDirectory();
File newFile = new File(file, "test.jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
catch (FileNotFoundException e) {e.printStackTrace();}
catch (IOException e) { e.printStackTrace(); }
}
}
When I run the app, nothing happens when I click the "Save" button. Doesn't download the image, nor does it crash. Since there is no error stacktrace, I can't see where I'm going wrong.
Make sure that you are calling this in your receiving fragment's onResume.
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
listener,
new IntentFilter("CUSTOM_ACTION")
);
and this in onPause
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(listener);
I' m using Picasso to help the cache of images.
The question is, how can I access the downloaded image to make a share intent?
any ideas? thanks!
I hope you can understand my question :-)
Sorry for my delay, I found a solution, but, not a good one...
First, I really searched for a while and looked at the code of Picasso. It seems like you should provide your own downloader and other stuff. But then, why should I use the lib...
And then, I suppose it's Picasso's design / architecture to just cache the file in the internal storage. Maybe because the external storage is not always available (like the user may plug in his SD card to his computer), or maybe because the external storage is not as fast as the internal... That's my guess. In a word, other apps cannot access the internal storage of the current app, so the share cannot be done.
Thus, I made a really ordinary solution. I just wait for Picasso to give the Bitmap, and compress it to a file in the external file, then do the share. It seems like a bad solution, but it really solves the problem, yes...
You should be aware of whether the external cache directory is available or not. If not, you cannot do the share. And you need to put the compress task in a background thread, so, waiting the external file cached... Does it seem like a bad solution? I think so...
Below is my project code, you can have a try...
private boolean mSaved; // a flag, whether the image is saved in external storage
private MenuItem mShare;
private Intent mIntent;
private ShareActionProvider mShareActionProvider;
private File mImage; // the external image file would be saved...
private Target target = new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
#Override
public void run() {
FileOutputStream os = null;
try {
String dir = CatnutUtils.mkdir(getActivity(), Constants.FANTASY_DIR); // check the exteral dir avaiable or not...
String[] paths = Uri.parse(mUrl).getPath().split("/");
mImage = new File(dir + File.separator + paths[2] + Constants.JPG); // resoleve the file name
} catch (Exception e) { // the external storage not available...
Log.e(TAG, "create dir error!", e);
return;
}
try {
if (mImage.length() > 10) { // > 0 means the file exists
// the file exists, done.
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage));
mSaved = true;
return;
}
os = new FileOutputStream(mImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
mIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mImage));
mSaved = true;
} catch (FileNotFoundException e) {
Log.e(TAG, "io error!", e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.e(TAG, "io closing error!", e);
}
}
}
}
}).start();
mFantasy.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
mFantasy.setImageDrawable(errorDrawable);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {
mFantasy.setImageDrawable(placeHolderDrawable);
}
}
};
#Override
public void onPrepareOptionsMenu(Menu menu) {
mShare.setEnabled(mSaved);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fantasy, menu);
mShare = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) mShare.getActionProvider();
mShare.setActionProvider(mShareActionProvider);
mShareActionProvider.setShareIntent(mIntent);
}
Finally, call Picasso.with(getActivity()).load(mUrl).into(target);
When the file is saved, the user can click the share menu do the share.
public static File getImageFile(Context context, String url)
{
final String CACHE_PATH = context.getCacheDir().getAbsolutePath() + "/picasso-cache/";
File[] files=new File(CACHE_PATH).listFiles();
for (File file:files)
{
String fname= file.getName();
if (fname.contains(".") && fname.substring(fname.lastIndexOf(".")).equals(".0"))
{
try
{
BufferedReader br=new BufferedReader(new FileReader(file));
if (br.readLine().equals(url))
{
File imgfile= new File(CACHE_PATH + fname.replace(".0", ".1"));
if (imgfile.exists())
{
return imgfile;
}
}
}
catch (FileNotFoundException|IOException e)
{
}
}
}
return null;
}
I've just found out this guide with a very good solution.
https://guides.codepath.com/android/Sharing-Content-with-Intents
The code will be like this:
// Can be triggered by a view event such as a button press
public void onShareItem(View v) {
// Get access to bitmap image from view
ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
// Get access to the URI for the bitmap
Uri bmpUri = getLocalBitmapUri(ivImage);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
It basically consists in retrieving the bitmap from the imageview and saving it to a local temp file and then using it for sharing. I've tested it and it seems to work fine.
In my application on clicking on button i'm showing alert dailog box to get file name.and getting name from user for a file.and saving it.
but what is happening is, after clicking on button alert dailog box is gets infront of screen and before that its try to save file.but its trying to save file befor user enters the name for file thats why file name is null in saving file.how to call use alert box so that i can get name from user and then save the file using that name.plase help me.
public void savebitmap(Bitmap bitmap)
{
str++;
AlertDialog.Builder alert = new AlertDialog.Builder(Work.this);
alert.setMessage("File name :");
input = new EditText(Work.this);
input.setLayoutParams(new LayoutParams(100,50));
alert.setView(input);
alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
NameValue = input.getText().toString();
System.out.println(" file name.---"+NameValue);
}
});
alert.show();
System.out.println("file is..."+NameValue);
try
{
System.out.println("in bitmap save...");
File fn=new File("/sdcard/"+" filename4"+".png");
FileOutputStream out=new FileOutputStream(fn);
System.out.println(",,,,,,,,,,,,,,,"+out);
Toast.makeText(getApplicationContext(), "In Save",Toast.LENGTH_SHORT).show();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90,out);
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "File is Saved in "+fn, Toast.LENGTH_SHORT).show();
}
catch(Exception e){
e.printStackTrace();
}
}
you will to move all file saving code inside onClick event of Button or wrap all code inside a method and then call it on Button click as :
public void savebitmap(Bitmap bitmap){
//....your code here
alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
NameValue = input.getText().toString();
System.out.println(" file name.---"+NameValue);
// put your code here to save file on Ok button click
saveFileOnSdCard(NameValue);
}
});
alert.show();
}
private void saveFileOnSdCard(String str_filename){
// move yor file saving code here..
}
You need to take input first then when user press OK on that you need to create file. So you need to write code in side onClick.
Try this,
public void savebitmap(Bitmap bitmap)
{
str++;
AlertDialog.Builder alert = new AlertDialog.Builder(Work.this);
alert.setMessage("File name :");
input = new EditText(Work.this);
input.setLayoutParams(new LayoutParams(100,50));
alert.setView(input);
alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
NameValue = input.getText().toString();
System.out.println(" file name.---"+NameValue);
try
{
System.out.println("in bitmap save...");
File fn=new File("/sdcard/"+" filename4"+".png");
FileOutputStream out=new FileOutputStream(fn);
System.out.println(",,,,,,,,,,,,,,,"+out);
Toast.makeText(getApplicationContext(), "In Save",Toast.LENGTH_SHORT).show();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90,out);
out.flush();
out.close();
}
catch(Exception e){
e.printStackTrace();
}
}
});
alert.show();
System.out.println("file is..."+NameValue);
}
I'm taking a screenshot from a layout that have a videoview, I have this code to save it, and to capture it, but when I open it I have a blank image but not an empty the image have 1.5Kb, can you help me?
this is how I capture the screenshot
public void TakePic(View v){
buton = AnimationUtils.loadAnimation(this, R.anim.but);
v.startAnimation(buton);
if (vid!=null)
{
if(vid.getCurrentPosition()!=0)
{
popupc = (LinearLayout) findViewById(R.id.guardapic);
popupc.setVisibility(View.VISIBLE);
LinearLayout layout = (LinearLayout)findViewById(R.id.videopic);
layout.setDrawingCacheEnabled(true);
layout.setDrawingCacheQuality(LinearLayout.DRAWING_CACHE_QUALITY_HIGH);
layout.buildDrawingCache();
bitmap = layout.getDrawingCache();
im=(ImageView)findViewById(R.id.imgdown);
// im.setImageResource(R.drawable.play_amarelo);
im.setImageBitmap(bitmap);
}
else
{
Toast toast = Toast.makeText(ctx,"Video has stopped...Restart", Toast.LENGTH_SHORT);
toast.show();
}
}
else
{
Toast toast = Toast.makeText(ctx,"Start video first", Toast.LENGTH_SHORT);
toast.show();
}
}
this is the code to save it into the sdCard
public void PicOk(View v){
String pathpic=null;
String nomepic=null;
EditText path= (EditText)findViewById(R.id.picpath);
EditText pic= (EditText)findViewById(R.id.nomepic);
pathpic=path.getText().toString();
nomepic=pic.getText().toString();
File folder = new File(Environment.getExternalStorageDirectory() + "/"+pathpic);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (!success) {
Log.d("Lino"," Pasta nao criada");
} else {
FileOutputStream ostream;
try {
File file = new File(folder.toString() + "/"+nomepic+ ".png");
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 95, ostream);
ostream.flush();
ostream.close();
// ((LinearLayout)findViewById(R.id.VV2)).destroyDrawingCache();
} catch (FileNotFoundException e) {
Log.d("Lino","erro"+e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d("lino","erro "+e.toString());
e.printStackTrace();
}
}
popupc = (LinearLayout) findViewById(R.id.guardapic);
popupc.setVisibility(View.GONE);
bitmap=null;
//tira foto
Toast toast = Toast.makeText(ctx,"pick taked", Toast.LENGTH_SHORT);
toast.show();
}
You know the video is actually Combination of Still Images. The moment when you take the picture the cross-ponding frame is blank. That is why screenshot appears to be black/blank.
So with this method you can't take the screen shot of a video. You need to adopt a different approach.
May be it helps you.
I am taking a screenshot of a RelativeLayout but it cause the error.
error:
05-10 17:43:44.249: ERROR/AndroidRuntime(7721): Caused by: java.lang.NullPointerException
05-10 17:43:44.249: ERROR/AndroidRuntime(7721): at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
05-10 17:43:44.249: ERROR/AndroidRuntime(7721): at com.API.Connect.share(FacebookConnect.java:110)
code:
public class Connect extends Activity implements LoginListener {
public static String news = " ";
RelativeLayout bitmapimage;
Bitmap bitmap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
bitmapimage = (RelativeLayout) findViewById(R.id.bitmapimg);
TextView txtlove = (TextView) findViewById(R.id.txtlove);
txtlove.setText(news.toString());
share();
}
public void share() {
View v1 = bitmapimage;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
saveImage();
// TODO Auto-generated method stub
}
void saveImage() {
String state = Environment.getExternalStorageState();
Environment.MEDIA_MOUNTED.equals(state);
File myDir = new File("/mnt/sdcard/DCIM/Camera");
myDir.mkdirs();
String fname = "mytry.jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
// create a Bitmap object from our image path.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
What you are trying to do is correct. But in onCreate() your view wouldn't have drawn. It's just inflated. So you might have to consider using the below method. try this out.
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
share(); //Call your share() here.
}
This method gets called once your view is drawn, and hence you will be able to get the bitmap by overriding this method.
I think i have found your error.
getdrawingcache already returns a bitmap, so you do not have to create a new one
that probably results into a nullpointer exception.
so you should try
bitmap = v1.getDrawingCache();
instead of
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
let me know if this works.
Greets,
Wottah