Can't set image int into webview? - android

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.

Related

How to print image displayed in ImageView in android Studio

I have a recyclerView which will display all images from JSON path and I used a click event that will display the clicked image to a new activity.
Now I want to print the image that is displayed. I have put a print button in the activity and while clicking on the print button i should get image to be printed in the network printer. I have used the below code. but I am getting error in getActivity() .
Button print = findViewById(R.id.print);
//imageView.setDrawingCacheEnabled( true );
print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PrintHelper photoPrinter = new PrintHelper(getActivity());
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
//Bitmap bitmap = imageView.getDrawingCache( );
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
photoPrinter.printBitmap("test print",bitmap);
}
});
Any help will be greatly appreciated :)
Use Activity.this instead of getActivity() where Activity is the name of your Activity.
The method getActivity() is used when you want to reference Fragments. But here you have an activity so there is an error! You have to do something like this:
//Other code
PrintHelper photoPrinter = new PrintHelper(YourJavaClassName.this);
//Other code
Please be sure to change YourJavaClassName to your activity's class name.
You can use getapplicationcontext() method or YourCurrentActivityName.this

Multiple ImageButton objects where each launches a different website

Suppose one of my screens has multiple ImageButton objects and each one should launch a unique website when clicked. For example, if the ImageButtons featured pictures of various restaurant logos (Burger King, Wendys, etc) when the ImageButton featuring the Wendys logo was clicked "wendys.com" would open. If the id of each ImageButton was something like
android:id="#+id/wendys"
is there any way that my launchWebsite() method in java could look something along the lines of
public void launchWebsite(View view) {
Intent openURL = new Intent(android.content.Intent.ACTION_VIEW);
openURL.setData(Uri.parse("https://www." + [insert id here] + ".com"));
startActivity(openURL);
}
Thanks so much in advance, I am relatively new to Android Studios
You would need to add a onClickListener for each ImageView and modify your method a little bit:
Change the parameter to pass a String (Do you need the view being passed? If yes, keep it there)
ImageButton ib_wendys = findViewById(R.id.ib_wendys);
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
launchWebsite("wendys") //I would even consider to pass the whole URL since it could be different than .com
}
});
Method:
public void launchWebsite(String url) {
Intent openURL = new Intent(android.content.Intent.ACTION_VIEW);
openURL.setData(Uri.parse("https://www." + url + ".com"));
startActivity(openURL);
}

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!

How to create an activity with its corresponding XML file just tapping a button

I'm programming an app which one of its functionalities is that the users can tap on a plus button (typical (+)...) that has to create a new activity and a new XML file with a specific structure.
I'm beginner on Android and also in Stack Overflow, so due to that I'm unable to post images and make this question easier to understand.
I need you to summarize how to program this. I'm not telling you to codify my code, I just need to know if it's possible to do and ,more or less, the steps to get it.
Hope you can help me. Thanks!
Okay, so this is how you'd do it, if you're sure you want to create a new instance of the activity from that very same activity. First, make a reference to your "plus button". Assuming the android:id="#+id/plusButton, it'd be like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Initialize your layout and variables
findViewById(R.id.plusButton).setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
}
}
This would launch a new instance of the same Activity (MainActivity) and give it focus.
You can make the activity yourself and have a button open the activity.
You'd start by making a new android activity. With eclipse it's simply File >> New >> Other >> Android Activity and then just fill out the form and hit finish. Make sure your current project is open.
Draw the button in your xml file, make sure it has a unique Id to reference and your text is declared in your strings.xml file that should look like this.
<string name="strX">(x)</string>
then in your activity's xml file under your button make sure you have
android:text="#string/strX"
You can also reference this in the GUI in the properties window under text.
With the button code in your .java you could use OnClickListener and Intent and the code for the button would look something like this.
TextView buttonYourButton = (TextView) findViewById(R.id.ButtonYourButtonId);
Button pushYourButton = (Button) buttonPlay;
pushYourButton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
Intent nameOfIntent = new Intent(NameOfCurrentClass.this, NameOfNewClass.class);
startActivity(nameOfIntent);
}
});
R.id.ButtonYourButton is the Id you gave the button, and the .class is the name of the public class in that .java file. Like:
public class MainActivity { ...
Anyways, good luck I was where you were about a month ago. Don't forget to check out tutorials on Android development on youtube, there are about a million of them. Also you can search stackoverflow for questions that already have been asked.

Categories

Resources