Capture a screenshot and share it with social media - java

Here is an excerpt of the code. I can compile it, but the program crashes on the phone/emulator.
Bitmap bitmap;
View v1 = MyView.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
String path = Images.Media.insertImage(getContentResolver(), bitmap,
"title", null);
Uri screenshotUri = Uri.parse(path);
final Intent socialIntent = new Intent(Intent.ACTION_SEND);
socialIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
socialIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
socialIntent.setType("image/png");
Anyone know how to do it? I want to capture a screenshot and let the user share it if he/she likes to. Everything else work, it's just the screenshot I cannot get.

The only way a screenshot can be obtained is by having direct access to the frame-buffer which is at the kernel level, this in turn requires rooted access to accomplish this and to pull in the data from there to make up the graphics that is the screen itself.
This requires either a modded ROM for this purpose or having root privilege to do so. Sony, I know, do it, they have that facility in place to do so without root as the ROM is modified, in the power menu, there is an option 'Take Screenshot'. CM is another that requires root.
There is a facility available in ICS that has the programmatic API available to do this, see this answer, but with earlier versions, you're out of luck.

Related

Receiving album art in android 11

As known, ALBUM_ART was deprecated in Android 11. Google says that ContentResolver.loadThumbnail should be used instead. But i totally don't understand how to use it, especially what i should provide as first parameter, uri. Documentation says:
Uri: The item that should be visualized as a thumbnail. This value cannot be null.
What is that item and how can I get it? Is this Uri of the music file?
You need to pass in the Uri of the track.
//This will get you the uri of the track, if you already have the track id
Uri trackUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, trackId);
Bitmap bm = contentResolver.loadThumbnail(trackUri, new Size(512,512), null);
Set the bitmap to an ImageView
imageView.setImageBitmap(bm);

Take picture and name it afterwards

I want to take a picture with the standart system service
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
and AFTERWARDS I want to give it a custom name and directory or path (which should happen in another activity after I've taken the photo with the camera activity)
The problem is that if I create a file with all the attributes (name, path) and give it to the intent I cant do it in the activity after having taken the photo instead I would need to determine its attributes before I open the Intent(take the photo).
(as suggested in the Google article:https://developer.android.com/training/camera/photobasics#TaskPath)
Should I just get the fullsize Bitmap then open the other activity and then save it and determine its attributes?
Is there a way to do it as suggested in the article but somehow the other way around?(create the File afterwards)
Let me know if you have any idea.
I apreceate any help.
Thank you!
I want to take a picture with the standart system service
Your code is launching any one of hundreds of possible camera apps. The specific app might have been pre-installed by a device manufacturer, or it might be an app that the user installed.
The problem is that if I create a file with all the attributes (name, path) and give it to the intent I cant do it in the activity after having taken the photo instead I would need to determine its attributes before I open the Intent(take the photo).
Correct.
Should I just get the fullsize Bitmap then open the other activity and then save it and determine its attributes?
There is no means of having ACTION_IMAGE_CAPTURE give you a "fullsize Bitmap" directly. You can either get a thumbnail-sized Bitmap or have it write a full-sized photo to a location of your choice.
Is there a way to do it as suggested in the article but somehow the other way around?(create the File afterwards)
No. However, there is nothing stopping you from opening the photo via its file in your activity, then modifying that photo and writing it back out. You could overwrite the original file, or you could write to some new location.
So, for example, if your concern is that you do not want the photo to be in a user-accessible location until your activity is done with it, you could pass a Uri to ACTION_IMAGE_CAPTURE that points to a private location in getCacheDir(), then have your activity write the final version of the photo to a file on external storage.

Sending image to Hangouts from local folder

I'm attempting to send an image to Hangouts from within an app I'm building.
I'm working in Xamarin for VS 2015 to do this so the code below is c# but it's not much different from the equivalent Java code so I think it's easy to follow.
What I've done is set up a button on my app which has code setting up an Intent to share an image to Hangouts. I've set the image up already in the Downloads folder on the device and hardcoded the name into the code.
Intent hangoutsShareIntent = new Intent(Intent.ActionSend);
hangoutsShareIntent.SetType("image/jpeg");
hangoutsShareIntent.SetPackage("com.google.android.talk");
string downloadsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
string filePath = Path.Combine(downloadsPath, "shared.jpg");
hangoutsShareIntent.PutExtra(Intent.ExtraStream, filePath);
StartActivity(Intent.CreateChooser(hangoutsShareIntent, "Share with"));
When I run this, I get the option to select a chat in Hangouts that I want to send the content to. Upon selecting the chat, I get a blank message box and no image.
I've swapped the above code over to use text/plain and pass the filePath variable to the message. When I copy the file path into Chrome to check it, the image loads so I have to figure that the image is where I've said it is... right?
I get no errors (probably because the issue is in Hangouts rather than my app so I have nothing to debug there). Logcat shows nothing except an error I can't find much about on Google: ExternalAccountType﹕ Unsupported attribute readOnly
The only information I could find on that error implied some issue with permissions but I've made sure my app has runtime permissions checked for Read/Write using this code (which wraps the above):
if ((CheckSelfPermission(Permission.ReadExternalStorage) == (int)Permission.Granted) &&
(CheckSelfPermission(Permission.WriteExternalStorage) == (int)Permission.Granted))
NOTE: I'm running this on a HTC One M8 - no SD card but does have external storage on device. I've also added the above permissions to the manifest for earlier Android versions.
The documentation for this (here) isn't overly helpful either so any advice AT ALL here is welcome :)
Thanks!
If you use the file provider instead of sending just the URI on its own. This should get around the permission issues you are seeing.
There is a guide available here which might be useful.
Intent shareIntent = new Intent(Intent.ActionSend);
shareIntent.SetType("image/gif");
Java.IO.File file = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/myimage.gif");
Android.Net.Uri fileUri = Android.Support.V4.Content.FileProvider.GetUriForFile(this, "com.myfileprovider", file);
shareIntent.SetPackage("com.google.android.talk");
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
shareIntent.PutExtra(Intent.ExtraStream, fileUri);
StartActivity(Intent.CreateChooser(shareIntent, "Share with"));

Sudden errors in Resources.java, Context.java and other android utility classes

I'm trying to work on a new project in Android Studio, and it's a NEW project because every time I try to run any app, at some point, the app will crash, and the debugger will show that there are "symbols" that it cannot resolve or find origins of in utility classes such as Resources and Context and BaseBundle (I don't even know what this class does) and oh so many more.
I'm trying to get extra content from and Intent
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
And here's when it crashes. At the get("data")
Build --> Clean Project and Build --> Rebuild Project didn't help.
File --> Invalidate Caches didn't help...
Starting a new project didn't help either...
Please Help
OP here, solved it!
This code initially worked when I tried to get an extra from the camera, and so I figured it would also work if I try to load a picture from the gallery.
Thing is, when you load something from your internal memory, it returns as a URI, which you need to convert to a Bitmap (In case you chose a photo).
the way to do it is as follows:
ImageView window = (ImageView) findViewById(R.id.imageView);
Uri imageUri = data.getData();
try {
Bitmap photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
window.setImageBitmap(photo);
}
catch(Exception e) { System.out.println(e.toString()); }
So apparently everytime the utility files get corrupt, it's because I'm doing something that can't be done, as in this case, it couldn't cast (Bitmap) data.getExtras().get("***"), and for obvious reasons.
Trouble is, it doesn't really tell you what the problem is, you need to Sherlock Holmes that SoB yourself.

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