My application keeps crashing every time I try to use the PhotoViewAttacher on my application of the PhotoView library. I'm using ION to display images and trying to get the PhotoView library to allow me to zoom into them. My code currently is:
public class massiveImageView extends Activity {
ImageView mImageView;
PhotoViewAttacher mAttacher;
Drawable drawable;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.massive_image_view);
// Any implementation of ImageView can be used!
mImageView = (ImageView) findViewById(R.id.imageViewMassivePage);
Ion.with(this).load("https://pbs.twimg.com/profile_images/522909800191901697/FHCGSQg0.png").withBitmap().asBitmap()
.setCallback(new FutureCallback<Bitmap>() {
#Override
public void onCompleted(Exception e, Bitmap result) {
// do something with your bitmap
drawable=new BitmapDrawable(result);
mImageView.setImageDrawable(drawable);
}
});
mAttacher = new PhotoViewAttacher(mImageView);
}
}
And the error I'm getting is at this line:
mAttacher = new PhotoViewAttacher(mImageView);
Saying:
java.lang.NoClassDefFoundError: uk.co.senab.photoview.PhotoViewAttacher
Any help would be hugely appreciated.
I still don't quite understand why it doesn't work, but I was able to find an answer, where instead of adding the dependency of the library, I just found the jar file online and added that instead, now everything is working perfect.
Library Link
Related
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);
}
});
}
I am new to google cardboard and trying to implement a very simple demo.
I have tried with VrVideowidget & panowidget separately but failed to implement it with GvrView. My work with code and output is given below.
I have added a GvrView which provide split screen automatically, after that i want to add same image/video into each screen so that it can appear well on google cardboard . The view I am adding via addView() appear single instead of two on both screen. I have added it on onDrawEye(). Please help me to reslove.
My final aim is to create a very basic demo of image/video on Google Cardboard with required feature.
MainActivity.java
public class MainActivity extends GvrActivity implements GvrView.StereoRenderer {
GvrView gvrView;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageView = new ImageView(this);
gvrView = new GvrView(this);
gvrView.setRenderer(this);
setContentView(gvrView);
}
#Override
public void onDrawEye(Eye eye) {
imageView.setImageResource(R.drawable.ic_launcher);
// gvrView.addView(imageView);
gvrView.addView(imageView,400,400);
}
}
Use VrPanoramaView instead, code sample is as follow:
add panowidget lib in your project
import com.google.vr.sdk.widgets.pano.VrPanoramaView;
VrPanoramaView panoWidgetView = (VrPanoramaView) findViewById(R.id.pano_view);
panoWidgetView.loadImageFromBitmap(img,null);
panoWidgetView.resumeRendering();
I am not an expert but my understanding is that you use onDrawEye to render images specific to the individual eyes, while you use onNewFrame() for non-eye specific rendering.
So moving
imageView.setImageResource(R.drawable.ic_launcher);
// gvrView.addView(imageView);
gvrView.addView(imageView,400,400);
to onNewFrame() should be enough.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imgView = (ImageView) findViewById(R.id.img_view);
Context context = this;
Picasso.with(this)
.load("http://i.imgur.com/DvpvklR.png")
.placeholder(R.drawable.appleadn)
.noFade()
.into(imgView);
}
I am new in android and i use picasso for download images but above with that code give me no error placeholder is also shown but image is downloaded or started yet
where i am wrong?
Or is there flag or something setting for picasso or OkHttp is must for use picasso?
Add this line in your manifest:
<uses-permission android:name="android.permission.INTERNET" />
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.
I've 3 fragments. Those fragments are placed into a TabsAdapter, for swipping between those fragments.
The problem, is that when the app loads, and the fragmentA view is created, it downloads the image and after it, changes a imageview:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.myFragmentView = inflater.inflate(R.layout.foto_setmana, container, false); //Això conté els "edittext i altres"
new DownloadImageTask(myFragmentView).execute("http://192.168.1.35/testing/fotos/foto1.jpg");
}
}
Code inside DownloadImageTask:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
View myfragmentview;
public DownloadImageTask(View myfragmentview) {
this.myfragmentview=myfragmentview;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
Log.d("debugging","mIcon11"+mIcon11.getHeight());
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
ImageView imv=(ImageView)this.myfragmentview.findViewById(R.id.imageView1);
imv.setImageBitmap(result);
}
So this is what is happening:
1.- I click the app
2.- The app loads. This screen is taken before the app had time to retrieve the image.
3.- As it retrieve the image, its shown.
4.- I swipe the fragment to the next one.
5.- The second fragment
6.- I swipe again to the 3rd fragment.
7. The 3rd fragment.
8. I go back to the first fragment, and the image isnt loaded anymore. Obviously I wont call to DownloadImageTask again, because it would slow so much users experience.
What should I do to preserve the image?
Oh, and by the way. If I just swipe from 1st to 2nd, the image isnt "unloaded", it just happens if I go to 3rd or further. Any idea why is this happening? I'm just curious about this.
Thank you!
Use the LruCache to create a RAM cache of your bitmaps.
This great Google IO talk explains exactly how to use it:
http://youtu.be/gbQb1PVjfqM?t=5m
I found out a way that it's working:
What we've to do, is creating a variable on the fragment to know if we downloaded, and another to save the bitmap at all, something like:
int downloaded;
Bitmap pictureSaved;
Then we initialize it on "onAttach" method from fragment.
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.downloaded=0;
}
And after that, on the "main code":
if (this.downloaded==0){
//we're here if we didn't download yet
new DownloadImageTask(myFragmentView, this).execute("http://192.168.1.33/testing/fotos/foto1.jpg");
this.downloaded=1;
} else if(this.downloaded==1) {
//we're here if we already downloaded.
ImageView imv=(ImageView)myFragmentView.findViewById(R.id.imageView1);
imv.setImageBitmap(this.pictureSaved);
}
DownloadImageTask code, should download the image, and as you can see, I pass to his constructor "this", so I can access the fragment methods and variables.
I create a method into the fragment to update the picture:
public void update(Bitmap updated){
ImageView imv=(ImageView)myFragmentView.findViewById(R.id.imageView1);
this.pictureSaved=updated;
imv.setImageBitmap(this.pictureSaved);
this.downloaded=1;
}
DownloadImageTask calls fragment's method like this.
Variables:
MyFragment frag;
Constructor:
public DownloadImageTask(View myfragmentview, MyFragmentA parent) {
this.myfragmentview=myfragmentview;
this.frag=parent;
}
PostExecute:
protected void onPostExecute(Bitmap result) {
frag.update(result);
}
It works perfectly.
Hope this helps someone.