I'm kind of new in android developing and i want to set home wallpaper with drawable image .
i wrote this code for setting but don't work and in visual android device at first i have dialog but next when i click as background i get this message :
"you need to insert sd card before using camera ..."
also i need use my drawable as source of images ...
here is my code :
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
MimeTypeMap map = MimeTypeMap.getSingleton();
String mimeType = map.getMimeTypeFromExtension("jpg");
Uri uri = Uri.parse("#drawable/wall_7");
intent.setDataAndType(uri,"image/jpeg" );
intent.putExtra("mimeType", mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
this.startActivityForResult(Intent.createChooser(intent,"Set as"),200);
and i going to have a slidable wallpaper , not a fix image . i'm using this intent bcs it let user crop image .
First of all you need to add this permession to your manifest.xml
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Then you can use this to set it using the wallpaperManager :
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MyActivity.this);
Drawable drawable = getResources().getDrawable(R.drawable.wallpaper_img);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
wallpaperManager.setBitmap(bitmap);
Or you can use this
to set the wallpaper using intent
You can directly set background in your xml file using android:background tag.Let me know if you have any further queries.
Related
welcome everyone
I'm trying writing a program to set the wallpaper, lock screen and contact picture etc from imageView by action attach data or any other way of doing the same.
Please see the pictures
image 1
image2
some code :
Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
setAs.setDataAndType(uri,"image/jpg");
setAs.putExtra("mimeType", "image/jpg");
startActivity(Intent.createChooser(setAs, "Set Image As"));
When the application runs out show me the message :
no app can perform this action
I add permission in manifest file :
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
Thank you very much for your time
I found solution , The cause of the problem is ( URI ) it should be this way :
Intent setAs = new Intent(Intent.ACTION_ATTACH_DATA);
// i : integer variable (index of array contains my images )
setAs.setDataAndType(Uri.parse(new File("/storage/emulated/0/pictures/wall/wall.jpg").toString()), "image/*");
setAs.putExtra("mimeType", "image/*");
setAs.addCategory(Intent.CATEGORY_DEFAULT);
setAs.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(setAs, "Set Image As"));
The above code does not set the image to the contacts .. I am continuing to try and if I find the solution I will put it here
We are trying to set the downloaded image (and stored inside galley under our own folder) as wallpaper using our application, and its working using the below code.
public void Set_Current_wallpaper() {
File f = new File(mCurrentWallpaperPath); // mCurrentWallpaperPath is Our folder inside gallery
Uri contentUri = Uri.fromFile(f);
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(contentUri, "image/*");
intent.putExtra("mimeType", "image/*");
startActivity(intent);
}
Problem is right now when above code is running below is how set wallpaper application screen looks like, But if we directly open the image from gallery and set as wallpaper, this is the wallpaper set screen looks like. Why is it opening in different way? Attaching the screens Actual vs Expected here.
Actual Result
Expected Result
Have you tried WallpaperManager?
WallpaperManager wallpapermgr = WallpaperManager.getInstance(this);
wallpapermgr.setBitmap(yourbitmap);
I have two methods that pick pictures for me. I want to do something like the Facebook profile picture square crop after choosing the picture. And was wandering if there is easy way like putting extras to the intent ("crop", "true"), ("aspectX/Y",1) and such. Right now I am experimenting with the intent extras, but can't make it work.
Constants.TAKE_CAMERA_PICTURE is 1000
Constants.SELECT_PICTURE_ACTIVITY_REQUEST_CODE is 1001
private void takeCameraPhoto() {
mPhotoHelper = PhotoHelper.recycle(mPhotoHelper);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = Uri.fromFile(mPhotoHelper.getPhotoFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, Constants.TAKE_CAMERA_PICTURE);
}
public void importPhotoAlbum() {
mPhotoHelper = PhotoHelper.recycle(mPhotoHelper);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, Constants.SELECT_PICTURE_ACTIVITY_REQUEST_CODE);
}
Thanks in advance!
I'm using jdamcd/android-crop library for this purpose. Just add this in your project as library..
Now to crop image just write one line i-e
Crop.of(source, dest).asSquare().start(this, CropCode);
here,
source is Uri of image file, dest is destination uri where image to be saved
It will do all the work for you.. Also there are other ways for cropping. Let me know if there is any difficulty for you :-)
If you want to use pure Android SDK , use the Bitmap class with the createBitmap method , check the docs Here , and check this example :
Bitmap croppedImage = Bitmap.createBitmap(sourceImage, startX, startY, destinationWidth, destinationHeight);
If you want to use Third party SDK, Take a look at this library Cropper Android widget for cropping and rotating an image ,i think it will do what you want ,check their wiki page with the following example :
1- add the CropImageView to your Layout XML file
<com.edmodo.cropper.CropImageView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/CropImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
2- you can modify attributes programmatically by using provided CropImageView methods:
CropImageView cropImageView = (CropImageView) findViewById(R.id.CropImageView);
cropImageView.setAspectRatio(5, 10);
cropImageView.setFixedAspectRatio(true);
cropImageView.setGuidelines(1);
3- To retrieve the image contained within the Cropper window, use the provided method getCroppedImage() to retrieve a Bitmap of the cropped image.
croppedImage = cropImageView.getCroppedImage();
ImageView croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
croppedImageView.setImageBitmap(croppedImage);
Hope That Helps.
I'm currently working on a app which the user can answer a question through images by taking photos or uploading photos from the album.
I've separated into two classes which are FulfillPhotoTaskActivity, AddPhotoActivity.
FulfillPhotoTaskActivity have imageView, addphoto button and save button.
when I press addphoto button, it goes to AddPhotoActivity which we can select two options(taking photos or uploading photos). I finished the part where if the user press take photo button then it opens the camera and take the photo. I created onActivityResult which get the image data. but in many example, inside the onActivityResult they also have imageView, like setImageBitmap.
My problem is, how can I get the imageView from the AddPhotoActivity and shows it in FulfillPhotoTaskActivity which I already made for XML.
When you take a photo it saves the image to a file. You can retrieve this image file and then you can call your imageView in FulfillPhotoTaskActivity and set the imageView to the image that is in the file.
The following code will start the camera intent and the file to a given location:
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder/SubFolder");
String folder = root.toString();
File file = new File(folder, "fileName" + ".jpg");
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 0);
Now when the user takes a picture you know where it will be saved. In your FulfillPhotoTaskActivity you can call your imageview:
ImageView imageView = (ImageView)findViewById(R.id.YOUR_IMAGE_VIEW_ID);
Finally, you can just set the image to the imageview:
imageView.setImageBitmap(BitmapFactory.decodeFile(file));
So, you don't need to get the imageview from AddPhotoActivity. You only need the file name. Then, you can go back to FulfillPhotoTaskActivity and set the imageview to the bitmap file.
I hope this helps!
I want to launch the camera from my application and want to use the captured image as an attachment in my application.
The use case is as below:
Press the capture button in the application->Opens camera application -> capture an image->the image is shown as thumbnail in my application.
Any suggestions, ideas, would be very helpful.
Image capture step description:
Get Path of image from ACTION_IMAGE_CAPTURE Intent
After that, you have path of the fetched image. Then you can use it for your purpose. Show it as small Bitmap, and attach the original to further processing.
To view captured image, send the intent
Intent viewImageIntent = new Intent();
viewImageIntent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(imageFilePath);
viewImageIntent.setDataAndType(Uri.fromFile(file), "image/*");
viewImageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(viewImageIntent);