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.
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 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();
}
I have a variable MyFinalPressure which is populated with sensor data from the sensors pressure. If MyFinalPressure is == to 4000 (4000 points or 40 sec) then stop writing to local storage.
But it looks like when I debug the code, it hits the boolean MaxPoints and is still writing. I wonder if my logic is wrong or not.
Could you please help me out.
public Boolean Store = false;
Boolean MaxPoints = false;
if (activity.Store) {
activity.writeToFile(MyFinalPressure);//MyFinalPressure is float of one dimension array or stream of array.
}
if (MyFinalPressure==4000){ //this conditon, am trying to stop wrting to local memory.
activity.MaxPoints = true;
}
FileOutputStream fileOutputStream;
//method to write into local memory.
public void writeToFile(final float MyFinalPressure) {
Log.d(TAG, "writeToFile.");
String finalData;
finalData = String.valueOf(MyFinalPressure);
try {
// true here for 'append'
fileOutputStream = new FileOutputStream(file, true);
String Space = " ";
byte[] convert = Space.getBytes();
fileOutputStream.write(finalData.getBytes());
fileOutputStream.write(convert);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//write to file.
StartWriting = (ToggleButton) findViewById(R.id.startWriting);
StartWriting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (StartWriting.isChecked()) {
Store = true;
Toast.makeText(getApplicationContext(), "Data Starts writing into (Message.txt) file", Toast.LENGTH_LONG).show();
} else {
if (!StartWriting.isChecked()|| MaxPoints==true) { //here - this is wrong logic to stop writing to my file.
Toast.makeText(getApplicationContext(), "Data Stored at myAppFile", Toast.LENGTH_SHORT).show();
String finalData1;
finalData1 = String.valueOf(fileOutputStream);
Log.i(TAG, "of_writes: " + finalData1);
// Toast.makeText(getApplicationContext(), "Data_write_number: " + finalData1.length(), Toast.LENGTH_SHORT).show();
Store = false;
}
}
}
});
Just take out !StartWriting.isChecked()
Because of the above if statement StartWriting.isChecked() will always be false. Then because you are checking for "!StartWriting.isChecked()" it will always enter the statement.
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.
I have successfully imported the library "com.ipaulpro.afilechooser.utils.FileUtils" to export and import the database on the SD card. But in class I get error on line:FileUtils.copyFile(internalDB, externalDB);
File internalDB = new File("/data/data/"+getPackageName()+"/databases/MyDatabase.db");
File externalDB = new File(Environment.getExternalStorageDirectory(), getPackageName()+"/database/MyDatabase.db");
try {
FileUtils.copyFile(internalDB, externalDB);
Toast toast = Toast.makeText(getApplicationContext(),(R.string.Toast_export), Toast.LENGTH_SHORT);
//toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
} catch (IOException e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(),(R.string.Toast_export_errore), Toast.LENGTH_SHORT);
toast.show();
}