Hello everyone I am creating an online wallpaper app.Where users can access wallpaper online.i want add two button first one is set as wallpaper and second one is download button .So if anybody help me about this I will be thankful to them thanks.
public class GalleryDetailActivity extends ActionBarActivity {
public static final String EXTRA_IMAGE = "extra_image";
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mImageView = (ImageView) findViewById(R.id.image);
if (getIntent() != null && getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey(EXTRA_IMAGE)) {
Picasso.with(this).load(getIntent().getExtras().getString(EXTRA_IMAGE)).into(mImageView);
}
}
First of all, you should make sure you have permission to make such action. Add in your manifest:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
To download the image hosted in some site you could use a 'doInBackground Thread' as suggested below:
How to download and save an image in Android
The button to set the wallpaper is created by code below:
Button setWallpaper = (Button)findViewById(R.id.YOUR_BUTTON);
ImageView imagePreview = (ImageView)findViewById(R.id.YOUR_PREVIEW);
imagePreview.setImageResource(YOUR_IMAGE_RESOURCE);
setWallpaper.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(YOUR_IMAGE_RESOURCE);
} catch (IOException e) {
e.printStackTrace();
}
}});
YOUR_IMAGE could be a local resource, like:
R.drawable.myImageFile
The link in the answer there are several ways of how to download your online image. Please, check it and try set up your wallpaper with local images first.
File f = new File(Environment.getExternalStorageDirectory(), "yourfile.jpg");
String path = f.getAbsolutePath();
File jpg = new File(path);
if(jpg.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(path);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
WallpaperManager m=WallpaperManager.getInstance(this);
try {
m.setBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
}
Related
I am using a Xamarin Forms application with Azure push notification. I need to redirect to a splash screen when my push notification is clicked. Android working fine. But in iOS a splash screen is not visible.
I tried the below example. But it's not hitting on the OnAppearing() method.
example
This is my splash screen code
public Splash(string PushNotification)
{
PushNotificationPage = PushNotification;
LoadSettings();
NavigationPage.SetHasNavigationBar(this, false);
var sub = new AbsoluteLayout {
BackgroundColor = Code.Application.Instance.CurrentReources.SplashScreenBackground
};
splashImage = new Image
{
Source = SplashImage
};
AbsoluteLayout.SetLayoutFlags(splashImage, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(splashImage, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
sub.Children.Add(splashImage);
if (Device.RuntimePlatform == Device.Android)
{
splashImage.HeightRequest = 270;
splashImage.WidthRequest = 270;
}
this.Content = sub;
}
protected override async void OnAppearing()
{
(App.Current as App).OnResumeHandler += Handle_OnResumeHandler;
base.OnAppearing();
splashImage.Opacity = 0;
await splashImage.FadeTo(1, 3000);
Xamarin.Forms.Application.Current.MainPage = new NavigationPage(new LoginPage(PushNotificationPage));
}
void Handle_OnResumeHandler(object sender, EventArgs e)
{
Console.WriteLine("OnPauseResumeWithPage");
}
protected override void OnDisappearing()
{
(App.Current as App).OnResumeHandler -= Handle_OnResumeHandler;
base.OnDisappearing();
}
Also added below method to App.cs
protected override void OnSleep()
{
OnSleepHandler?.Invoke(null, new EventArgs());
}
protected override void OnResume()
{
OnResumeHandler?.Invoke(null, new EventArgs());
}
I don't know this is programmatically good or bad. But its work for me now. I have call OnDisappearing() method inside splash method. Its loading only if my push notification clicked. so its work for me without issue.
if(PushNotification!=null)
{
OnAppearing();
}
I'm using apache pdfbox in my javaFx application where im reading a pdf document now i want to display a org.apache.pdfbox.pdmodel.PDDocument inside a pane in my FXML. So far i tried with org.apache.pdfbox.PDFReader but it is using it's own Jframe. I want to show it inside a pane.
Here what i have done so far
public class CustomPDFReader extends PDFReader {
public CustomPDFReader(BillModel bm) {
super();
showAllPages(bm.getAllPages());
setVisible(true);
}
private void showAllPages(List<PDPage> pagesList) {
try {
Field documentPanel = getClass().getSuperclass().getDeclaredField("documentPanel");
documentPanel.setAccessible(true);
JPanel panel = (JPanel) documentPanel.get(this);
GridLayout layout = new GridLayout(0, 1);
panel.setLayout(layout);
for(PDPage page : pagesList) {
PageWrapper wrapper = new PageWrapper(this);
wrapper.displayPage(page);
panel.add(wrapper.getPanel());
}
pack();
} catch(Exception e) {
e.printStackTrace();
}
}
}
I want onStart() method to load image from server using picasso and I want to show a progress bar until the photos are fully downloaded
Here is my code:
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Picasso.with(context).load(imageLoad)
.placeholder(R.id.progressBarDetails)
.error(R.drawable.friend_request).noFade().resize(200, 200)
.into(avatarImage, new Callback() {
#Override
public void onError() {
// TODO Auto-generated method stub
}
#Override
public void onSuccess() {
// TODO Auto-generated method stub
progressbar.setVisibility(View.GONE);
}
});
Picasso.with(this).load(imageLoad).into(target);
}
OnFinished a = new OnFinished() {
#Override
public void onSendFinished(IntentSender IntentSender, Intent intent,
int resultCode, String resultData, Bundle resultExtras) {
// TODO Auto-generated method stub
intent = new Intent(getApplicationContext(), Map.class);
}
};
private Target target = new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
#Override
public void run() {
File file = new File(Environment
.getExternalStorageDirectory().getPath()
+ "/actress_wallpaper.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 75, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
I haven't tested your code but even if that works, the file actress_wallpaper.jpg isn't loaded in the ImageView. In the docs, it says
Objects implementing this class must have a working implementation of Object.equals(Object) and Object.hashCode() for proper storage internally.
Try this:
File file = new File(pathToFile);
Picasso.with(context)
.load(file)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
progressbar.setVisibility(View.GONE);
}
});
be warned I haven't tested my code.
Update:
I have tried version 2.3.2 and 2.3.3, it seems like that there's an issue https://github.com/square/picasso/issues/539
It is an old question but may be this answer can help others as I also had issues in showing progress bar while loading image from server.
I am using Picasso 2.4.0. and I am using Picasso Target interface to load image in imageview. Here is the tested and working code:
First add the following lines:
ImageView ivPhoto = (ImageView) findViewById(R.id.iv_photo);
ProgressBar pbLoadingBar = (ProgressBar) findViewById(R.id.pb_loading_bar);
//get image url
String imageUrl = getImageUrl();
//ImageViewTarget is the implementation of Target interface.
//code for this ImageViewTarget is in the end
Target target = new ImageViewTarget(ivPhoto, pbLoadingBar);
Picasso.with(mContext)
.load(imageUrl)
.placeholder(R.drawable.place_holder)
.error(R.drawable.error_drawable)
.into(target);
Here is the implementation of Target interface used above
private static class ImageViewTarget implements Target {
private WeakReference<ImageView> mImageViewReference;
private WeakReference<ProgressBar> mProgressBarReference;
public ImageViewTarget(ImageView imageView, ProgressBar progressBar) {
this.mImageViewReference = new WeakReference<>(imageView);
this.mProgressBarReference = new WeakReference<>(progressBar);
}
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//you can use this bitmap to load image in image view or save it in image file like the one in the above question.
ImageView imageView = mImageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
ProgressBar progressBar = mProgressBarReference.get();
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
ImageView imageView = mImageViewReference.get();
if (imageView != null) {
imageView.setImageDrawable(errorDrawable);
}
ProgressBar progressBar = mProgressBarReference.get();
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
ImageView imageView = mImageViewReference.get();
if (imageView != null) {
imageView.setImageDrawable(placeHolderDrawable);
}
ProgressBar progressBar = mProgressBarReference.get();
if (progressBar != null) {
progressBar.setVisibility(View.VISIBLE);
}
}
}
The above code works fine if used for loading image in activity. But if you want to load image in gridview/recyclerview or view pager etc. where same view holder is used, you might get an issue where onBitmapLoaded() is not called (as the view is recycled and Picasso only keeps a weak reference to the Target object). Here is a link to solve this problem.
change to this
Picasso.get()
.load(tImageUrl())
.into(holder.AnimImage, new Callback() {
#Override
public void onSuccess() {
holder.progressBar.setVisibility(View.GONE);
}
#Override
public void onError(Exception e) {
}
});
I am search an pagination example for loading data asynchron from a server. I have no clue how to solve that with the pagination control of javafx. Well i hava an example where an observable list is loaded in the background with 10k of items. But i only want to load the items for a page when its actually needed. So only when the user switcht to the next page i want to grab the next 20 items with a task. When the task is done the page should be rendered..
Thanks for any advice and help!
Link to observable example:
https://forums.oracle.com/forums/thread.jspa?messageID=10976705#10976705
All you need is to start a background thread with your task once user clicked on a page. See next example which uses sites downloading for a long task:
public class Pages extends Application {
#Override
public void start(Stage primaryStage) {
final Pagination root = new Pagination(urls.length, 0);
root.setPageFactory(new Callback<Integer, Node>() {
// This method will be called every time user clicks on page button
public Node call(final Integer pageIndex) {
final Label content = new Label("Please, wait");
content.setWrapText(true);
StackPane box = new StackPane();
box.getChildren().add(content);
// here we starts long operation in another thread
new Thread() {
String result;
public void run() {
try {
URL url = new URL(urls[pageIndex]);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(1000);
urlConnection.setReadTimeout(1000);
BufferedReader breader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = breader.readLine()) != null) {
stringBuilder.append(line);
}
result = stringBuilder.toString();
} catch (Exception ex) {
result = "Download failed";
}
// once operation is finished we update UI with results
Platform.runLater(new Runnable() {
#Override
public void run() {
content.setText(result);
}
});
}
}.start();
return box;
}
});
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Pages!");
primaryStage.setScene(scene);
primaryStage.show();
}
private final static String[] urls = {"http://oracle.com", "http://stackoverflow.com", "http://stackexchange~.com", "http://google.com", "http://javafx.com"};
public static void main(String[] args) {
launch(args);
}
}
I am using the support library to create dialogs using fragments.
And i Have the following code to show and dismiss the dialogs:
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("Create", "Create");
setContentView(R.layout.activity_report);
init();
addListeners();
addhandlerListener();
super.onCreate(savedInstanceState);
}
private void showDialog(final Class<?> classs) {
if (classs.equals(AddressValidateProgress.class)) {
addressValidateProgress = AddressValidateProgress.newInstance();
addressValidateProgress.show(getSupportFragmentManager(), null);
Log.d("counter", "+1");
}
if (classs.equals(GPSSearchProgress.class)) {
showDialog(gpsSearchloadId);
}
}
private void dismissDialog(final Class<?> classs) {
if (classs.equals(AddressValidateProgress.class)) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.remove(addressValidateProgress).commitAllowingStateLoss();
addressValidateProgress = null;
Log.d("super", addressValidateProgressId + ":dismissed");
}
if (classs.equals(AddressChooseDialog.class)) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.remove(addressChooseDialog).commitAllowingStateLoss();
addressChooseDialog = null;
}
if (classs.equals(GPSSearchProgress.class)) {
dismissDialog(gpsSearchloadId);
Log.d("super", gpsSearchloadId + ":dismissed");
}
}
If i start the application in portrait mode i can use the dialogs normally, i can even rotate the screen and the dialogs are reconstructed.
The problem is that if i start the application rotate the screen and click the button that open the dialogs i get an exception:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Solution found.
It was caused by a static Handler declared