- Finding a button by it’s «tag» in android
- Android java get html image tag from string
- Android java get html image tag from string
- How to read png images form inputstream in java
- How to extract image url from a string?
- How to convert image to string in Android?
- android java get html image tag from string
- Solution 2
- Solution 3
- Solution 4
- how to get value from xml tag in android
- Set and get using tag a fragment in android
Finding a button by it’s «tag» in android
I am making an android app where initially the user get the «GO» button. On clicking the button a new relative layout is made visible which has a few option buttons namely — «addition» and «subtraction». I then try to get which button the user has clicked and accordingly present that set of questions. However, as soon the the GO button is clicked the app crashes.. My code to check which button in clicked is —
public void start(View view)
04-24 22:21:39.525 10853-10853/com.example.rohit_136.brain_trainer I/YO:: Start button ok! 04-24 22:21:39.527 10853-10853/com.example.rohit_136.brain_trainer E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.rohit_136.brain_trainer, PID: 10853 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5226) at android.view.View$PerformClick.run(View.java:21350) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5582) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5226) at android.view.View$PerformClick.run(View.java:21350) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5582) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com.example.rohit_136.brain_trainer.MainActivity.start(MainActivity.java:177) at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5226) at android.view.View$PerformClick.run(View.java:21350) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5582) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-24 22:21:39.535 10853-10865/com.example.rohit_136.brain_trainer I/ActivityThreadEui: schedulePauseActivity com.example.rohit_136.brain_trainer.MainActivity finished=true userLeaving=false configChanges=0 dontReport=false 04-24 22:21:41.098 10853-10853/com.example.rohit_136.brain_trainer I/Process: Sending signal. PID: 10853 SIG: 9
Android java get html image tag from string
Solution 2: You’ve escaped the double quotes in the string catenation so the regex engine sees this after c++ parses the string. Use See this demo Java demo: Note that requires a full string match, while will find a partial match, a match inside a larger string.
Android java get html image tag from string
Please see this question for reference. Basically it says to use:
String imgRegex = "]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
I use jsoup. It is pretty easy to use and lightweight. Some versions were not Java 1.5 compatible but it appears they fixed the issue.
String html = str; Document doc = Jsoup.parse(html); Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png
Frist of All Import jsoap:
compile group: 'org.jsoup', name: 'jsoup', version: '1.7.2'
private ArrayList pullLinks(String html) < ArrayList links = new ArrayList(); Elements srcs = Jsoup.parse(html).select("[src]"); //get All tags containing "src" for (int i = 0; i < srcs.size(); i++) < links.add(srcs.get(i).attr("abs:src")); // get links of selected tags >return links; >
Android — Retrofit API to retrieve a png image, You could also use Retrofit to perform the @GET and just return the Response. Then in code you can do isr = new BufferedInputStream (response.getBody ().in ()) to get the input stream of the image and write it into a Bitmap, say, by doing BitmapFactory.decodeStream (isr). Share. answered Feb …
How to read png images form inputstream in java
In client application, read the InputStream via Socket.getInputStream() method.
BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); BufferedImage image = ImageIO.read(in);
Android SDK does not support the method ImageIO.read(). Even if you can compile your code, your android application will get crashed and have error about missing libraries like this:
could not find method javax.imageio.imageio.read
What I suggest is using bitmapping instead of this.
Java — Get base64 String from Image URI, java android base64. Share. Improve this question. Follow edited Nov 4, 2019 at 18:36. halfer . 19 2- Bitmap to Base64 String conversion: ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); byte[] …
How to extract image url from a string?
You seem to have some issue with the regex, the \» + \» should come from some code you mistook for a regex. That subpattern requires a quote, one or more spaces, then a space, and another double quote to appear right before the extension. It matches something like http://www.medivision360.com/pharma/pages/articleImg/thumbnail/thumb3756d839adc5da3″ «.jpg .
Also, there are two redundant capture groups at the beginning, you do not need to use them.
String regex = "https?:/(?:/[^/]+)+\\.(?:jpg|gif|png)";
String rx = "https?:/(?:/[^/]+)+\\.(?:jpg|gif|png)"; String url = "http://www.medivision360.com/pharma/pages/articleImg/thumbnail/thumb3756d839adc5da3.jpg"; Pattern pat = Pattern.compile(rx); Matcher matcher = pat.matcher(url); if (matcher.matches())
Note that Matcher#matches() requires a full string match, while Matcher#find() will find a partial match, a match inside a larger string.
You’ve escaped the double quotes in the string catenation
so the regex engine sees this (http(s?):/)(/[^/]+)+» + «\.(?:jpg|gif|png)
after c++ parses the string.
You can un-escape it «(http(s?):/)(/[^/]+)+» + «\\.(?:jpg|gif|png)»
or just join them together «(http(s?):/)(/[^/]+)+\\.(?:jpg|gif|png)»
If the expression is always at the end, I would suggest:
Java — How do I read pixels from a PNG file?, This should work: javax.imageio.ImageIO.read (new File («filename.png»)) Then you can walk through the pixels and compare with the images pixel by pixel with this: java.awt.image.BufferedImage.getRGB (int x, int y). Share. edited Nov 2, …
How to convert image to string in Android?
private String getBase64String() < // give your image file url in mCurrentPhotoPath Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // In case you want to compress your image, here it's at 40% bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); return Base64.encodeToString(byteArray, Base64.DEFAULT); >
private void decodeBase64AndSetImage(String completeImageData, ImageView imageView) < // Incase you're storing into aws or other places where we have extension stored in the starting. String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1); InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT)); Bitmap bitmap = BitmapFactory.decodeStream(stream); imageView.setImageBitmap(bitmap); >
Java — Convert text to image file on Android, I want to convert it to an image (.png or .jpg). For example, black text on white background. How can I do that programmatically? Stack Overflow. About; Products For Teams; Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with …
android java get html image tag from string
Please see this question for reference. Basically it says to use:
String imgRegex = "]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
Solution 2
I use jsoup. It is pretty easy to use and lightweight. Some versions were not Java 1.5 compatible but it appears they fixed the issue.
String html = str; Document doc = Jsoup.parse(html); Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png
Solution 3
Frist of All Import jsoap:
compile group: 'org.jsoup', name: 'jsoup', version: '1.7.2'
private ArrayList pullLinks(String html) < ArrayList links = new ArrayList(); Elements srcs = Jsoup.parse(html).select("[src]"); //get All tags containing "src" for (int i = 0; i < srcs.size(); i++) < links.add(srcs.get(i).attr("abs:src")); // get links of selected tags >return links; >
Solution 4
An XMLPullParser can do this pretty easily. Although, if it is a trivially small string, it may be overkill.
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xpp = factory.newPullParser(); xpp.setInput( new StringReader ( "I have string like this with
some HTMLtag with image tags in it. how can get it ?" ) ); int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) < if(eventType == XmlPullParser.START_TAG && "img".equals(xpp.getName()) < //found an image start tag, extract the attribute 'src' from here. >eventType = xpp.next(); >
how to get value from xml tag in android
I have made a parser,in that i am getting all the values of that xml tag,But i want to get some values which are inside tag as an argument,I dont know how to get it from tags,The sample xml is as below: sample.xml
,I want to get value of «averageRate»,»nightlyRateTotal»,»total»,But I have no idea how to get that data?My parser is as below:
while (eventType != XmlPullParser.END_DOCUMENT && !done) < tagName = parser.getName(); switch (eventType) < case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if (tagName.equals(ITEM)) < rssFeed = new RSSFeed_SelectedHotelResult(); >if (tagName.equals(DESCRIPTION)) < name = parser.nextText().toString(); hotel_name_list.add(name); System.out .println(". DescriptionL:lLLLLLLLLLL" + name); >if (tagName.equals(OFFER)) < offer = parser.nextText().toString(); hotel_offer_list.add(offer); System.out.println(". Offer" + offer); >if (tagName.equals(HOTEL_IMG)) < thumbNailUrl = parser.nextText().toString(); hotel_image_list.add(thumbNailUrl); >if (tagName.equals(HOTEL_LOCATION)) < locationDescription = parser.nextText().toString(); hotel_lOCATION_DESC.add(locationDescription); System.out .println(". ;Location deccription. ;;" + locationDescription); >if (tagName.equals(HOTEL_RATE_IMG)) < tripAdvisorRatingUrl = parser.nextText().toString(); >if (tagName.equals(HOTEL_PRICE)) < lowRate = parser.nextText().toString(); >if (tagName.equals(HOTEL_RATING)) < hotelRating = parser.nextText().toString(); hotel_rate_list.add(hotelRating); >if (tagName.equals(HOTEL_ADDRESS)) < address1 = parser.nextText().toString(); hotel_address_list.add(address1); System.out .println(". hotel address. " + address1); >if (tagName.equals(DESTINATION_ID)) < destinationId = parser.nextText().toString(); dest_id_list.add(destinationId); System.out .println("*********Here Is DestinationID:************" + destinationId); >if (tagName.equals(CITY)) < city = parser.nextText().toString(); city_list.add(city); System.out.println("*********Here Is City:************" + city); >if (tagName.equals(HOTEL_ID)) < hotelId = parser.nextText().toString(); hotel_id_list.add(hotelId); System.out .println("*********Here Is HotelID:************" + hotelId); >if (tagName.equals(LATITUDE)) < latitude = parser.nextText().toString(); latitude_list.add(latitude); System.out .println("*********Here Is Latitude:************" + hotelId); >if (tagName.equals(LONGITUDE)) < longitude = parser.nextText().toString(); longitude_list.add(longitude); System.out .println("*********Here Is Longitude:************" + hotelId); >break; case XmlPullParser.END_TAG: if (tagName.equals(CHANNEL)) < done = true; >else if (tagName.equals(ITEM)) < rssFeed = new RSSFeed_SelectedHotelResult(name, thumbNailUrl, tripAdvisorRatingUrl, hotelRating, locationDescription, lowRate, destinationId, city, hotelId, latitude, longitude, offer); rssFeedList.add(rssFeed); >break; > eventType = parser.next(); > > catch (Exception e) < e.printStackTrace(); >return rssFeedList; >
Set and get using tag a fragment in android
I’ve created a tab layout with viewpager. Everything was alright, except that I need to run a method in a specific moment. So I need to get fragment instance and run their method. I create in this way:
@Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); activateToolbarWithNavigationView(HomeActivity.this); // Tabs Setup TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); final ViewPager viewPager = (ViewPager) findViewById(R.id.home_pager); if (tabLayout != null) < tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.favorites_label_fragment)).setTag(getString(R.string.fragment_favorite_tag))); tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.air_today_label_fragment)).setTag(getString(R.string.fragment_airing_today_tag))); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); final HomePageAdapter adapter = new HomePageAdapter (getSupportFragmentManager(), tabLayout.getTabCount()); if (viewPager != null) < viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() < @Override public void onTabSelected(TabLayout.Tab tab) < viewPager.setCurrentItem(tab.getPosition()); >@Override public void onTabUnselected(TabLayout.Tab tab) < >@Override public void onTabReselected(TabLayout.Tab tab) < >>); > > public void refreshFavorites()
I don’t know if i’m doing it in wrong way, or there some mistake that they return null from findFragmentByTag. I can’t figure out. In case, I’ve checked some others answers but I can’t understand what I really need to do. viewpager adapter:
public class HomePageAdapter extends FragmentStatePagerAdapter < int mNumOfTabs; public HomePageAdapter(FragmentManager fm, int NumOfTabs) < super(fm); this.mNumOfTabs = NumOfTabs; >@Override public Fragment getItem(int position) < switch (position) < case 0: FavoritesFragment favoritesFragment = new FavoritesFragment(); return favoritesFragment; case 1: AirTodayFragment airTodayFragment = new AirTodayFragment(); return airTodayFragment; default: return null; >> @Override public int getCount() < return mNumOfTabs; >>
public void refreshFavorites() < ListallFragments = getSupportFragmentManager().getFragments(); for (Fragment fragmento: allFragments) < if (fragmento instanceof FavoritesFragment)< ((FavoritesFragment) fragmento).executeFavoriteList(); >> >
EDIT 2: WHERE I USE: I didn’t use refreshFavoretes inside my Activity but actually in Fragments that are inside of it:
@Override public void onClick(View v) < . // Refresh Favorites if (getActivity() instanceof MainActivity) ((MainActivity) getActivity()).refreshFavorites(); >