Glide - How load image before dialog show? - android

I'm working with Glide for server loading images. I need to load image before some dialog shows.
What is the best approach?
EDIT: This is an example from my code:
On my dialog class
#Override
protected void onCreate(final Bundle savedInstanceState) {
setContentView(R.layout.dialog_alert);
ButterKnife.bind(this);
int size = mActivity.getResources().getDimensionPixelSize(R.dimen.product_icon);
String productThumbnailUrl = api.getThumbnailUrl(mProduct.getImage(), size, size);
Glide.with(mActivity)
.load(productThumbnailUrl)
.placeholder(R.drawable.icon_default)
.into(mImage);
}
On my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
[...]
AlertProduct dialog = new AlertProduct(this, product);
dialog.show();
}

Loading into targets should do what you want.
If you simply want to load a Bitmap so that you can interact with it
in some special way other than displaying it directly to the user,
maybe to show in a notification, or upload as a profile photo, Glide
has you covered.
SimpleTarget provides reasonable default implementations for the much
larger Target interface and let's you focus on handling the result of
your load.

Related

Android ImageView From URL: Not Getting Set (Using Picasso)

I'm brand new to Android development, so I'm probably doing something stupid. Any ideas why this code shouldn't work?
It compiles and runs, but doesn't actually successfully set the image (which is previously set to a gray background).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
Button getImageButton = (Button) (findViewById(R.id.btnGetImage));
getImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView myImageView = (ImageView) (findViewById(R.id.myImageView));
Picasso.with(v.getContext()).load("https://www.example.com/someimage").into(myImageView);
}
});
}
alter you code this way
Picasso.with(YOUR_ACTIVITY_NAME.this).load("https://www.example.com/someimage").into(myImageView);
Here v.getContext() is not related to the main context, so it won't help you, we need to pass context of current activity
Please try this and let me know.
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
Button getImageButton = (Button) (findViewById(R.id.btnGetImage));
ImageView myImageView = (ImageView) (findViewById(R.id.myImageView));
getImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Picasso.with(YourActivityName.this).load("https://www.example.com/someimage").into(myImageView);
}
});
}
Ding ding here is my answer
I am sure that you add the dependency
compile 'com.squareup.picasso:picasso:2.5.2'
and used the right url
Picasso.with(v.getContext()).load("http://i.imgur.com/DvpvklR.png").into(imageView);
I am telling you where you went wrong !!
Magic : Have you added the internet permission ? It needs to be in your manifest
<uses-permission android:name="android.permission.INTERNET" />
Rather than that your code is just fine
But i recommend you to use getApplicationContext()
copying from my comment
Picasso is a library and not an application. While creating
Picasso instance if you'll not pass context, then how do you think
it will get the application context from ? For it to work it requires
context , and it definitely needs to be provided by the application
using this library.It also prevents leaking an Activity (if that's
what you pass ,some of below answers have used ) by switching to the
application context. use getApplicationContext()
And for the people who randomly put an answer here read this as well
View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.
Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside
of). Use this instead of the current Activity context if you need a
context tied to the lifecycle of the entire application, not just the
current Activity.
ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The
Context referred to from inside that ContextWrapper is accessed via
getBaseContext().
so any way he is accessing the currant activity context.
Cheers!
step 1 In Menifest check Internet Connection
<uses-permission android:name="android.permission.INTERNET" />
step 2 compile "com.squareup.picasso:picasso:2.4.0"
step 3 Image url is not showing any image in browser paste your url in browser and if you get image then you can set that image to imagview
step 4 check url is working or not there is not .png or .jpg extension for imagfile
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
Button getImageButton = (Button) (findViewById(R.id.btnGetImage));
getImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Initializing the ImageView
imageView = (ImageView) findViewById(R.id.imageView);
//Loading Image from URL
Picasso.with(this)
.load("https://www.example.com/someimage")
.placeholder(R.drawable.placeholder) // optional
.error(R.drawable.error) // optional
.resize(400,400) // optional
.into(imageView);
}
});
}

Android remove Bitmap from Memory when leave activity

I am developing an app that needs a image gallery. i am at the point where i have all the images displaying in a grid view. when i touch an image i want it to open an activity with the touched image full screen, which is also working but when i go back to the grid and open another image the memory consumption keeps growing. i have tried setting the feature image to null, Bitmap.recycle() and calling finish() on the activity none of which seem to stop the memory consumption increasing when i open a different image.
Open Detail Activity
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("image", adapter.file[position].getPath());
//Start details activity
startActivity(intent);
Feature Image Activity
public class DetailActivity extends AppCompatActivity {
private Intent intent = getIntent();
private Bitmap featureImage;
private ImageView featureView;
private final String TAG ="Image Detail --";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extras = getIntent().getExtras();
final String imgPath = extras.getString("image");
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inDensity = 2;
bitmapOptions.inTargetDensity = 1;
featureImage = BitmapFactory.decodeFile(imgPath, bitmapOptions );
featureView = (ImageView) findViewById(R.id.featureImageView);
featureView.setImageBitmap(featureImage);
}
#Override
protected void onPause(){
super.onPause();
//featureImage = null;
//featureView.setImageBitmap(null);
//featureImage.recycle();
this.finish();
}
}
the memory continues to grog up to about 40MB and then drops to 32MB and cycles throgh in this pattern.
Any tipps/suggestions are welcome.
The problem is that you're not scaling the images down to fit the ImageView properly. You have two options:
Read and implement what's shown in this lesson. The benefits of this approach are you only use the code you need, the drawback is that it's quite a lot of work for something that should be straight forward.
Use a library like Glide. It's simple to use and well supported. The drawback is that it does add many methods to your project that you otherwise wouldn't use, adding to the apk size.
Personally, whenever I've got to display an image, I use Glide!

Trying to load pictures from url using novoda ImageLoader

hi I am trying to load an image from an url using novoda Direct ImageLoader method
right now I have this class
public class MainActivity extends Activity {
public class DirectLoading extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv=(ImageView) findViewById(R.id.imageView1);
ImageView ivv=(ImageView) findViewById(R.id.imageView2);
Bitmap b=new DirectLoader()
.download("https://upload.wikimedia.org/wikipedia/commons/a/ad/SmallStellatedDodecahedron.jpg");
Bitmap pic=new DirectLoader()
.download("http://www.coetail.com/mamitakagi1129/wp-content/themes/twentyten/images/headers/cherryblossoms.jpg");
ivv.setImageBitmap(pic);
iv.setImageBitmap(b);
}
private void guiBuilder() {
}
}
}
I should get 2 images into the 2 imageViews, but I am getting a blank screen. There is a "Hello world" string in the layout that is not displayed so I guess I do get the images but they are not displayed graphically.
As it says in readme file https://github.com/novoda/ImageLoader/blob/develop/README.md you should handle threading when using DirectLoader.download(), for example, using AsyncTask.

Android Imageview displays old bitmap after screen rotates

My app is supposed to first displays an image, when a button is pressed, another image is displayed in the image view. However, when the screen is rotated, the imageView displays the old image. How should I go about fixing this? (bits is the Bitmap loaded into imageView on create)
My code is below:
RGBToBitmap(rgb.getWindow(), bits); //this loads a new image into bits
imageView.setImageBitmap(bits);
I suppose you are setting the first image of your ImageView in the onCreate or onStart method of your Activity.
Upon rotating the screen, the onCreate and onStart methods get called again, and therefore your ImageView displays the first image again.
In order to save your Activity state, have a look at this:
http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
This could be a possible solution:
Bitmap image = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
image = (Bitmap) getLastNonConfigurationInstance();
if(bitmap == null){
image = downloadImage();
}
setImage(bitmap);
}
#Override
public Object onRetainNonConfigurationInstance() {
return bitmap;
}
You can also read into this method when ur using fragments:
Fragment.setRetainInstance
Or the Bundle: (onSaveInstanceState(Bundle) is also available in your Activity)
//Save it onSaveInstanceState:
#Override
public void onSaveInstanceState(Bundle toSave) {
super.onSaveInstanceState(toSave);
toSave.putParcelable("bitmap", bitmap);
}
//nd get it back onCreate:
#Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
if (savedState != null) bitmap = savedState.getParcelable("bitmap");
}

Can't set image int into webview?

Allright so here's my problem. I have an app which contains alot of pictures and i wanted to be able to pinchzoom these, but since the ImageViewer doesnt support this natively I thought I'd use the Webviewer instead, but heres the thing. all my pictures are saved into my "picture" int and its the picture function i want to load into the webviewer. not a specific \drawable\bla.jpg Searched all around but didnt find anything about it. I'll attach some code for reference.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
int picture=intent.getIntExtra("picture", 22);
setContentView(R.layout.pictureframe);
ImageView image = (ImageView) findViewById(R.id.pansarvagn);
image.setBackgroundResource(picture);
and its here where i want smth like
Webview image = (WebView) findViewById(R.id.pansarvagn);
image.(setdata bla bla)
and this is the picture function
public void displayPicture(int pictureresource){
Intent intent = new Intent();
intent.putExtra("picture", pictureresource);
intent.setClass(getApplicationContext(), Picture.class);
startActivity(intent);
called by
Button tank = (Button) findViewById(R.id.tank);
tank.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayPicture(R.drawable.tank);
}
To further elaborate i want to put an image into a webview which i will be able to zoom into. so some sort of imageview inside the webview to fill up the webview with my picture. and then I want to be able to zoom in on it.
You need to create you own ContentProvider and override it's opeenFile(..) method. Then you can feed it to WebView:
myWebView.loadUrl("content://your.content.provider/someImageName");
Here is an example: http://www.techjini.com/blog/2009/01/10/android-tip-1-contentprovider-accessing-local-file-system-from-webview-showing-image-in-webview-using-content/
Note: with this approach you will need to save your images somewhere. You will not be able to serve them from memory.

Categories

Resources