I use below code to set photo as wallpaper:
try {
File f = new File(PhotoPath);
InputStream in = new FileInputStream(f);
this.setWallpaper(in);
} catch (IOException e) {
e.printStackTrace();
}
For some photo, it can set as wallpaper success.
But for some photo, the wallpaper will be set to device default wallpaper or all black.
Is there any limitation of the photo which be set to wallpaper?
Or this code how to modify to fix issue?
Try this method from this link : developer.
public void setStream (InputStream data);
Currently image must be either a JPEG or PNG. This method supports from API Level 5.
Related
I have developed an android app in which I have implemented a background service using AlarmManager, which executes after every 20 seconds and change the home screen wallpaper of device, its working fine. But, there is an issue which is very frequent, the android Wallpapermanager mix up 2 images (one is device default wallpaper) and start showing the combination of 2 images on my home screen. if home screen wallpaper is set as scrolling wallpaper. you can see in attached image, which is very annoying. I have tried to fix this up, by increase service execution delay but all in vain. Will you please suggest me a suitable solution for this. here is my code
final WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
Bitmap bm = null;
File imgFile = new File(imageURL);
if(imgFile.exists()){
bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
}
if(bm == null) return;
try {
myWallpaperManager.setBitmap(bm);
bm.recycle();
} catch (IOException e) {
e.printStackTrace();
}
I want to upload a photo from my camera directly into a drive folder:
OutputStream outputStream = result.getDriveContents().getOutputStream();
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
/* image is my Bitmap */
image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
So im doing this and it's working. The problem is that my pictures in drive is in very low quality and i need to have a High Quality video.
I already tried this solution Converting bitmap to byteArray android
But then in Drive the photo wasnt recognize as media file and can't read it. I may have failed something.
EDIT: i've done exactly the same things that is there https://stackoverflow.com/a/13000774/6644403 and doing this way to get my Bitmap in ActivityResult :
try {
mBitmapToSave = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
For Get Actual Image predefined path of Captured Image using
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
this.startActivityForResult(cameraIntent, 101);
After Captured Image you can get Captured Image on path which is set in cameraIntent. If you don't Want to set predefined path then check Intent data.
if (resultCode == android.app.Activity.RESULT_OK && requestCode == 101) {
try {
path_mm = "Onsuccess_resultcode";
generateNoteOnSD("photo34.txt", path_mm);
Bitmap photo = null;
if (data == null) {
//get Bitmap here.
} else {
Uri u1 = data.getData();
//get uri and find actual path on that uri.
}
}catch(Exception ex) {}
}
Refer this link Upload large files to Google Drive using GD API for google drive Uploadation.
I wasnt asking for rights to WRITE_EXTERNAL_STORAGE, it was in my manifest but since api23 i need to explicit and ask if the user ok.
Now it's good.
i'm using these codes to set my image from server as wallpaper :
WallpaperManager wpm = WallpaperManager.getInstance(MainActivity.this);
InputStream ins;
try {
String xlx = "http://myserver.com/myimage.jpg";
ins = new URL(xlx).openStream();
wpm.setStream(ins);
} catch (IOException e) {
e.printStackTrace();
}
it's working and my image will set as wallpaper,
but my problem is that it is not setting full image as wallpaper, so how should i do to set full image as wallpaper without cropping?
set imageview property in your layout xml file.
android:scaleType="fitXY"
here is the problem: i have searched for an answer for this and so far i made it work for the custom camera app that comes with htc phones.
i have the folowing
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {
InputStream is=null;
File file=mInterface.getTempFile();
try {
is=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(is==null){
try {
u = data.getData();
is=getContentResolver().openInputStream(u);
mInterface.saveStringPreferences(GlobalInterface.URI_SAVENAME, u.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//Now "is" stream contains the required photo, you can process it
setImage(is);
}
//and this is the code to the function that calls the intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(mInterface.getTempFile()));
startActivityForResult(intent, REQUEST_FROM_CAMERA);
the remaining functions getTempFile() is to create a temp file on /sdcard/blabla.tmp
and a function to set the captured image in an image view.
so the problem is that i tested this code on the custom camera on a lg phone and it crashes. the question is: is there a way to get an uri for the image location (i dont need a specific save location it could be the default set from the camera) and there were also some issues i came across which i dont need to solve like getting a smaller resolution image. i just need one image and to be able to set it in an imageView and the method to work for every camera app that is there, custom or native regardless.
is this possible or i need to create a new camera class for this? in my opinion the camera apps that come with the phone are more adjusted to the phone model so i would prefer using them against building a custom one.
tnx.
Try this:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Source
Is it possible to set the android wallpaper image programatically? I'd like to create a service that downloads an image from the web and updates the home screen wallpaper periodically.
If you have image URL then use
WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();
wpm.setStream(ins);
If you have image URI then use
WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setResource(Uri.of.image);
In your manifest file:
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
From this page on the developer site:
public void setStream (InputStream data)
Change the current system wallpaper to a specific byte stream. The give InputStream is copied into persistent storage and will now be used as the wallpaper. Currently it must be either a JPEG or PNG image.
If you have bitmap of image than you will add this function to set as wallpaper:
public void SetBackground(int Url) {
try {
File file = new File("/sdcard/sampleimage");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Url);
bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
Context context = this.getBaseContext();
context.setWallpaper(bitmap);
Toast.makeText(getApplicationContext(), "Wallpaper has been set", Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
you should add permission for this
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
hope it will work
OK Here's how to do it before api 2.0:
You need to call getApplicationContext.setWallpaper() and pass it the bitmap.
This method is now deprecated. See ChrisF's answer for details on the new method.