libGDX: Android doesn't save screenshot - java

The problem is that when you click, for example, on the display, a screenshot of the screen should be saved, but this does not happen, the image simply is not in the gallery or elsewhere, while if you test for the desktop version, the screen is successfully saved. I am making a drawing for android with saving work, but I do not know how else to save a picture on android.
if (Gdx.input.isTouched())
{
byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);
for (int i = 4; i < pixels.length; i += 4)
{
pixels[i - 1] = (byte) 255;
}
Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888);
BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length);
PixmapIO.writePNG(Gdx.files.local("mypixmap.png"), pixmap);
local = Gdx.files.getExternalStoragePath().toString();
System.out.println(Gdx.files.getLocalStoragePath());
System.out.println(Gdx.files.getExternalStoragePath());
pixmap.dispose();
}

Gdx.files.local is only for your internal files and can't be accessed by gallery.
You need to use Gdx.files.external, but the current libGDX version uses a path here that is also not accessible under normal circumstances. It is changed for the next version.
Depending on what you want to achieve, there are other ways to get going with platform specific code.
Do you want to save screenshots periodically? Use app-specific storage from 1.9.12
Do you want to share the screenshot to another app? Use local storage and androidx.core.content.FileProvider
Do you want to open a dialog and the user can save the image? Use Storage Access Framework

Related

Processing for Android | save() "File contains a path separator"

I'm using Processing 3.5.4.
I’m trying to save() an image of the screen to data/frames (relative to my sketch file). The code I’m using works in Java mode with no problems (I can see the image saving into the correct folder on my computer), but when running it on my Android device I get java.lang.IllegalArgumentException: File data/frames/frameasdf.tif contains a path separator. I’m guessing this is because of a difference in file storage systems.
Is there any way I can avoid path separators other than saving images directly into the sketch folder?
I’m new to Java (just moved over from Javascript for more professional development), so if possible, please link to any helpful documentation.
PImage drawing;
void setup() {
size(displayWidth, displayHeight);
}
boolean clicked = false;
String name = "asdf";
void mouseReleased() {
clicked = true;
}
void draw() {
background(255);
if(drawing != null) {
image(drawing, 0, 0);
}
fill(0);
noStroke();
ellipse(mouseX, mouseY, 50, 50);
if(clicked) {
save("data/frames/frame" + name + ".tif");
drawing = loadImage("frames/frame" + name + ".tif");
}
clicked = false;
}
This is a shortened version of my code. It's a simple program that should add a dot to the screen whenever you click.
I do plan on saving more than one frame in the frames folder.
I didn't understand your question very well. Are you trying to save the file inside the specific app files or in shared storage files? the root files of the different storage systems can be got with Context.getExternalFilesDirs() that returns a vector with the files.
you can start reading the documentation here: Android storage

is it right to use batcher.drawSprite to draw all images in game?

I'm developing a game and use batcher.drawSprite method to draw all images in the game (background and all characters)
in assets.java :
charAtlas = new Texture(game, "charAtlas.png");
charEnemy = new TextureRegion(charAtlas, 0,0,250,300);
in worldGame.java :
batcher.beginBatch(Assets.charAtlas); // set atlas
batcher.drawSprite(130, 628, 120,140, Assets.charEnemy);
//assets.charEnemy
is it right to use this method in all condition ?
I have 3 atlas in game , i even use 2048x2048 atlas size so i can include all my images in there..
However, the image looks blurry in game (Tested in galaxy note, tab, and galaxy young). looks at the code above, i even have the enemy char take size in my atlas as much as 250x300 , it's not make sense that it'll look blurry as i only draw it in 120x140.
note : i use no layout (i mean no layout file in res folder) .. i use drawsprite to draw all image (Character,menu, button, etc)..
update :
I tried to use character image files from other game that i unzipped, when i run the app, it also looks blurry and jagged. while in the original game, it's so smooth and sharp. why is that ?
check your code, you might use scaledbitmap , see if you can set like this.
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);

Is it possible to get a higher resolution from getDrawingCache?

I am using SubsamplingScaleImageView by Dave Morrissey (https://github.com/davemorrissey/subsampling-scale-image-view) to allow users to crop and pan a photo with gestures.
I modified the library to add a tint and a logo to the photo. Now I need to upload the photo to the server. In order to do that I need to somehow extract the photo from the SubsamplingScaleImageView.
I added the following method to the SubsamplingScaleImageView class:
/**
* Capture a photo of the image view
*/
public Bitmap getOutput() {
buildDrawingCache();
Bitmap b1 = getDrawingCache();
Bitmap b = b1.copy(Config.ARGB_8888, true);
destroyDrawingCache();
return b;
}
I am using this method to get the file, resize it to a specific resolution (800x800), and save it to my app's folder.
The problem I noticed is that the extracted photo from the drawing cache depends on the resolution of the device. For example, on my Full HD device I get 1080x1080 photo, which is enough, but on some lower res devices I get resolutions like 480x480 and that is not enough since the image needs to be bigger than that, so the photo gets blurry when I resize it to 800x800.
Is there a way to get the same resolution photo from that image view on all devices?

displaying a custom map and placing pin/images on the map

i wanted to create a directory app for a grocery. i already have the floor plan of the place. i just wanted to display it in a view and i want to put some markers/pins/images to it.. i have no idea where to start. someone told me to use bitmap and canvass but i have no idea how to use it as the other tutorials in google is quite vague and hard to understand..
what my app requires:
display a custom map from an image source
enable me to put markers,pins and some images on top of it
A simple way is to use use ImageView:
//make a bitmap from your floorPlanImage, example from file
Bitmap floorPlanImage = BitmapFactory.decodeFile(filePath, options);
//make a mutable bitmap, so it can be changed
Bitmap mutableFloorPlanImage = floorPlanImage.copy(Bitmap.Config.ARGB_8888, true);
//create a canvas to draw on your floorPlanImage
Canvas mCanvas = new Canvas(mutableFloorPlanImage);
//draw a marker or another image on the floorPlanImage, you can position it using left and top //coordinates, in this example both 0
mCanvas.drawBitmap(markerBitmap, 0, 0, null);
//show the resulting bitmap in ImageView
imageView.setImageBitmap(mutableFloorPlanImage);

Take Screenshot of Android screen and save to SD card

There are a few questions here on SO about capturing screenshots of an android application. However, I haven't found a solid solution on how to take a screenshot programatically using the android SDK or any other method.
So I thought I would ask this question again in the hopes that I can find a good solution, hopefully one that will allow capturing full length images that I can save to the SD card or somewhere similar.
I appreicate any help
This is not possible directly on the device/emulator, unless it is rooted.
to honest all I need it for is the emulator as this is for a testing application on a PC
This sounds like a job for monkeyrunner.
monkeyrunner tool can do the job for you with bit of adb command, [python script]
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
//waits for connection
device = MonkeyRunner.waitForConnection()
//take the current snapshot
device.takeSnapshot()
//stores the current snapshot in current dir in pc
device.writeToFile('current.png')\
//copy it to the sd card of device
os.subprocess.call('adb push current.png /sdcard/android/com.test.myapp/current.png')
Note: call this jython script file
monkeyrunner.bat <file name>
You will most likely not be happy with this answer, but the only ones that I have seen involve using native code, or executing native commands.
Edit:
I hadn't seen this one before. Have you tried it?:
http://code.google.com/p/android-screenshot-library/
Edit2: Checked that library, and it also is a bad solution. Requires that you start the service from a pc. So my initial answer still holds :)
Edit3: You should be able to save a view as an image by doing something similar to this. You might need to tweek it a bit so that you get the width/height of the view. (I'm inflating layouts, and specify the width/height when I layout the code)
View content = getView();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
File file = new File(pathAndFilename);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
You can look at http://codaset.com/jens-riboe/droidatscreen/wiki (with a write up at http://blog.ribomation.com/2010/01/droidscreen/): this is a Java library that uses adb to capture a screen shots. I've been able to (with a lot of elbow grease) modify the source to let me automatically capture a timed series of screen shots (which I use for demo videos).
You can see the class structure at http://pastebin.com/hX5rQsSR
EDIT: You'd invoke it (after bundling all the requirements) like this:
java -cp DroidScreen.jar --adb "" --device "" --prefix "" --interval

Categories