I am building a wallpaper app in java. When user selects an image, a small default menu should pop up like in the provided screenshot, where user can select which app should set the wallpaper.
So far I used WallpaperManager for setting the wallpaper but that doesnt give user an option to move the image etc. This is my first android app ever so I am learning with every step I take, so sorry if this is an easy task. I also have not found SO question that solves my problem.
Can you please point me to the right direction, waht should I google, look for, or show me some code that does this? Thank you in advance.
the followin code my solve your problem
File f=new File("wallpaper_file_path");
Intent i=new Intent(Intent.ACTION_SET_WALLPAPER,Uri.fromFile(f));
startActivity(i);
I made a wallpaper app, in that, I used a zoom image library so that I can adjust the image then I take the cache of that image view are and set it as wallpaper. and I can set it for the home screen, lock screen and for both
public void SetHomeAndLockScreen()
{
bmap=((BitmapDrawable)imageview.getDrawable()).getBitmap();//get the cache from zoomable imageview
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
WallpaperManager wpm = WallpaperManager.getInstance(getApplicationContext());
try {
wpm.setBitmap(bmap, null, true, WallpaperManager.FLAG_LOCK);
wpm.setBitmap(bmap);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Wallpaper Applied", Toast.LENGTH_SHORT).show();
}
I was understand that you need to crop image from gallery try this use phone screen width for x and screen height for y
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 4);
intent.putExtra("aspectY", 16);
intent.putExtra("outputX", 720);
intent.putExtra("outputY", 1080);
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CROP_ICON);`
When the picture select Activity return will be selected to save the contents.in
onActivityResult:
Bundle extras = data.getExtras();
if(extras != null ) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
// The stream to write to a file or directly using the photo
} and set the wall paper using wallpaper manager
Modern solution :)
/**
* Set as wallpaper method
*/
public void setWallpaper() {
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // Android 7.0+ required
Uri wallUri = getImageUri(this, wallpaperDrawableBitmap);
intent.setDataAndType(wallUri, "image/jpeg");
intent.putExtra("mimeType", "image/jpeg");
this.startActivity(Intent.createChooser(intent, "Set as:"));
}
Modern solution for static contexts :)
/**
* Set as wallpaper
*/
public static void setWallpaper(Activity activity) {
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // Android 7.0+ required
Uri wallUri = getImageUri(activity, wallpaperDrawableBitmapStatic);
intent.setDataAndType(wallUri, "image/jpeg");
intent.putExtra("mimeType", "image/jpeg");
activity.startActivity(Intent.createChooser(intent, "Set as:"));
}
Note that setDataAndType(...) requires the image URI.
Related
I want to display the image when I click on the photo and want to set in my ImageView without user select yes or not....
I had searched for it and I also know it very well that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it. But, I want to do it without review/retake the activity display it.....
I am trying this code fine
Initialise
Uri mImageCaptureUri;
For Click on Button
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(MediaStore.AUTHORITY, true);
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = null;
mPath = mImageCaptureUri.getPath();
System.out.println("THE PAtH:_" + mPath);
BitmapFactory.Options o2 = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(mPath, o2);
ivSelfie.setImageBitmap(bitmap);
}
When I am Click the Photo Than I am Take this screen to select yes or not......
But My requirement is not select review/retake task and direct set to ImageView on activity display when just click and set.....
Actually it's quite useful to have confirmation of taken picture. But, in case if you really don't want to have it, you have to use SurfaceView inside your app and show camera stream here. There is tones of example how to do it, for example consider to check that one.
Use method setImageURI() it will get the bitmap from the uri and set it for you.
Yes it will set the image weather the user press ok or cancel no matter because your file exists on your given path while launching intent.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_CAMERA) {
// only one line code
ivSelfie.setImageURI(mImageCaptureUri);
}
}
You cannot avoid that screen. The reason is that when you use new MediaStore.ACTION_IMAGE_CAPTURE you are using a different camera application for clicking image. Showing this screen might be the default functionality of the. This screen will be different in different devices depending upon the camera application.
So to get rid of this the only thing you can do is to implement your custom camera instead of using default camera application.
As per default camera app you can't breach that functionality. Instead of, you can use SurfaceView to capture image inside you application. Please have a look at this answer thread.
May be this link will help you.
Thank you
Use "android.intent.extra.quickCapture" for your picture intent.
// File ImagePickerModule.java#248
cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("android.intent.extra.quickCapture",true);
See https://github.com/react-native-community/react-native-image-picker/issues/647 for details.
I'm developing an Android application to capture screenshots. What I'm trying to do is make a service with a button on the top of the screen and taking screenshot by tapping it.
I wonder if it may work, with service as I said. Also I want the button excluded from the screenshot. Am I able to do this?
Here is the code that allowed my screenshot to be stored on sd card and used later for whatever your needs are:
First, add proper permission to save file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
And this is the code (running in an Activity):
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// 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 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
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();
}
}
And this is how you can open the recently generated image:
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
to exclude the button, you can make button hide and then take screen show after some time interval.
I know that this question has been asked several times before, I am trying to add caption to image shared to instagram using send intent
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"YOUR TEXT TO SHARE IN INSTAGRAM");
shareIntent.setPackage("com.instagram.android");
return shareIntent;
Has someone ever managed to make it work?
Is it not supported or has the support been revoked?
There was an official statement from Instagram (mid-2015) announcing that pre-populated captions would no longer be accepted in the iOS and Android apps:
Beginning today, the iOS Hooks and Android Intents will stop accepting captions passed by third party apps. This is a non-breaking change: existing mobile apps that utilize pre-filled captions will continue to be able to use this flow to share media through the Instagram apps, but now Instagram will ignore the caption text. To create a caption for a photo or video shared by a third party app, users will have to enter a caption manually, the same way they already do when sharing content using the Instagram native apps.
Looking at the Instagram documentation for Android, indeed we see that there's no mention of providing the conventional Intent.EXTRA_TEXT string extra in the intent as is customary for other apps. Their sample is limited to only providing a Uri:
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
I'm sorry to say that it simply isn't possible, and we're at the discretion of Facebook in making this decision.
Until it`s not solved by Instagram, I copy the text to the clipboard and instruct the user to paste it
#Override
public void onSingleImageSelected(Uri uri, String tag) {
fileProfileImage = uri.getPath();
compressProfileImage();
imgShareTosocial.setVisibility(View.VISIBLE);
Glide.with(getApplicationContext()).load(uri).into(imgShareTosocial);
}
#SuppressLint("CheckResult")
private void compressProfileImage() {
File file = new File(fileProfileImage);
new Compressor(this)
.compressToFileAsFlowable(file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<File>() {
#Override
public void accept(File file) throws Exception {
compressProfileImage = file;
String imagePath = compressProfileImage.getAbsolutePath();
tvSelectMedia.setText(imagePath);
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
}
});
}
private void shareToInstagram() {
path = tvSelectMedia.getText().toString().trim();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
if (intent != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
try {
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), path, "Step Up", "Step Up")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
shareIntent.setType("image/jpeg");
startActivity(shareIntent);
} else {
// bring user to the market to download the app.
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.instagram.android"));
startActivity(intent);
}
}
I'm with the same problem. I think is not possible at this time.
In https://instagram.com/developer/mobile-sharing/android-intents/ only talk about Intent.EXTRA_STREAM, so i suppose that it's the only available.
Here is my code:
Intent instagramIntent = new Intent(Intent.ACTION_SEND);
instagramIntent.setType("image/*");
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
instagramIntent.putExtra(Intent.EXTRA_STREAM, uri);
instagramIntent.setPackage("com.instagram.android");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(instagramIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
if(resolveInfo.activityInfo.packageName.startsWith("com.instagram.android")){
instagramIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name );
resolved = true;
break;
}
}
if(resolved){
startActivity(instagramIntent);
}else{
Toast.makeText(PromocionarMain.this, "Instagram App is not installed", Toast.LENGTH_LONG).show();
}
Instagram have stopped accepting pre-populated capitions to increase the quality of content in the system. See this post.
http://developers.instagram.com/post/125972775561/removing-pre-filled-captions-from-mobile-sharing
Hi I have the following code to share an image:
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri uri = Uri.parse(getFilesDir() + File.separator + "myGoal.jpg");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
It works to share the image to Dropbox but if I pick the Facebook option, I get Facebook's status update dialog with no image attached and if I try to update my status with 'Test' it doesn't work. No errors. Just not working.
I know it's not the image because it uploads to my Dropbox properly and I can pull up the image and look at it.
Do I have to attach the image to the intent differently for it to work with Facebook?
Any ideas? I'm debugging on a physical device.
So I figured out the problem.
I was saving the picture to internal storage with getFilesDir() which put the picture into my apps sandbox and made inaccessible to the other apps.
I replaced my code with the following:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/MyApp/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "myPic.png");
FileOutputStream fOut = new FileOutputStream(file);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "My Image");
startActivity(Intent.createChooser(share, "Share Image"));
Works perfectly fine now.
What i have done to post image on facebook is instead of passing it directly i create a function and write it like this :
private void ShareWall(String message) {
Bundle parameters = new Bundle();
// share msg
parameters.putString("message", message);
// shre image which you have define above
parameters.putString("picture", postImage);
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("response: ", response);
if (response == null || response.equals("")) {
response.equals("");
showToast("No Response.");
} else {
showToast("Message has been posted to your walll!.");
}
finish();
} catch (Exception e) {
showToast("Message failed to posdt on wall.");
e.printStackTrace();
finish();
}
}
Hope this help you.
You can still share images (but not text) from your app to Facebook even if you are not using the Facebook SDK.
Just make sure that you use Uri.fromFile instead of Uri.parse and it will work:
DO NOT USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathToFile));
USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pathToFile)));
Here is a solution without using external file write:
Drawable mDrawable = myImageView1.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
In this case, my image comes from an ImageView myImageView.
I am using the camera by creating an intent:
Intent cameraI = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
and then starting it with:
startActivityForResult(cameraI, request);
where request is a predefined constant integer.
I then have the onActivityResult():
public void onActivityResult(int req, int res, Intent data) throws NullPointerException
{
try{
super.onActivityResult(req, res, data);
if(req == request)
{
Bitmap picture = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(picture);
}//if
else
{
Toast.makeText(getApplicationContext(), "No picture taken", 4);
}//else
}catch (NullPointerException e){
String error = "null pointer exception";
Toast.makeText(getApplicationContext(), error, 4);
}
}//onActivityResult
My predicament is that I do not have a camera on this computer. I get eclipse's default checkerboard and moving square animation. Does this actually function? I have been clicking the "take picture" button but nothing is happening. What I want to happen is to take a single picture, and then return it to the activity that called i. Is that the default action or do I have to specify that I want it to return after taking one picture?
Thanks in advance for any help.
YEah, the newer emulators(2.3.3) should be fine for testing.
Some devices have the problems of returning data in different formats. Instead, what you could do is, pass a location(you can choose) to the camera app, and it would save the new image to that location, and you can just blindly use the location that you have specified.
For example
File tempFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/myfolder");
tempFolder.mkdir();
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfolder",
String.valueOf(System.currentTimeMillis()) + ".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, TAKE_PICTURE);
I'm not sure about the simulator and what it is capable of, but here is a tutorial that you might find helpful:
http://achorniy.wordpress.com/2010/04/26/howto-launch-android-camera-using-intents/
The code in question ended up working. Upon testing on a device, the picture was returned as desired. Eclipse's emulated camera simply did not work. If i recall correctly you can enable a camera, but that is a different discussion entirely.