how to display either video or image from response? - android

I am getting a response from JSON API where a "Video" tag contains a video or image name with its extension as shown below. I want to show video if that tag contains video (I am loading video in Web view) then hide Image view, or show image if it contains image then hide Web view.
Problem coming is that webview visibility is gone if the response postion has .mp4 and image view is visible but response image is not showing in Picasso.
Response:
[
{
"Video": "8100931.mp4"
},
{
"Video": "218519.jpeg",
}]
Layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/llVideoPost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<WebView
android:id="#+id/wvPostVideo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:focusable="false"
android:screenReaderFocusable="false"
android:visibility="visible"
app:layout_constraintDimensionRatio="1:1"
tools:ignore="MissingConstraints" />
<ImageView
android:id="#+id/imgPostImage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:contentDescription="#string/todo"
android:scaleType="fitXY"
android:visibility="gone"
app:layout_constraintDimensionRatio="1:1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried this code
Code for calling response:
String videoTag= businessList.get(position).getVideo();
if (businessList.get(position).getVideo().endsWith(".mp4")) {
holder.wvPostVideo.setVisibility(View.VISIBLE);
holder.imgPostImage.setVisibility(View.GONE);
holder.wvPostVideo.loadUrl("https://smakerspace.s3.ap-south-1.amazonaws.com/Video/" + videoTag);
} else if (businessList.get(position).getVideo().endsWith(".jpeg")) {
holder.wvPostVideo.setVisibility(View.GONE);
holder.imgPostImage.setVisibility(View.VISIBLE);
Picasso.get()
.load("https://smakerspace.s3.ap-south-1.amazonaws.com/upload/"+videoTag)
.rotate(90)
.into(holder.imgPostImage);
}

replace https://smakerspace.s3.ap-south-1.amazonaws.com/upload/ with https://smakerspace.s3.ap-south-1.amazonaws.com/Video/
Becuase you have stored these image and video file into one db folder
And please use Videoview for video player its make eassy to play a video into android

Instead of passing url to picasso, can you please first try to download the image programmatically and then try to set to any ImageView.
Or you can use any rest clients to validate if APIs are working fine and returning valid image.
Once above is validated and found to be working fine, next step is to check if we got right instance of imgPostImage

Related

Using placeholder in Coil when the url is null

I get some data from database and push it to reciclerview. I use Coil to show images. But there are some urls that are null and I need to use some placeholder in such cases.
I tried all the option I saw in the internet but nothing helped.
binding.posterIv.load(UtilsUi.BASE_URL + recyclerViewItem.poster_path){
placeholder(R.drawable.placeholder_male)
error(R.drawable.placeholder_male)
fallback(R.drawable.placeholder_male)
}
When the url is null I just get empty black space.
ImageView looks like this:
<ImageView
android:id="#+id/actorImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/keanuriives" />
Do you have any idea how to fix it?
P.S. Coil needs to be used, not Glide or Picasso.

How to set image to Google Play Books API thumbnail using Fresco?

I am trying to load in a Google Play books image thumbnail from a URL using Fresco (I've also tried using Glide and Picasso) 1.10.0. I am able to load in another image (such as this https://i.imgur.com/6zDqjm8.jpg), but only the placeholder image shows when I use the Google Play Books thumbnail link (e.g. http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api).
I've also tried adding ".jpeg" on to the end of the link (since in a web browser the image still shows) in the Uri.parse code, but it doesn't make a difference.
Do you know why this is and/or what I am doing wrong?
I have got the INTERNET permission in my manifest. I also have the Fresco namespace in my XML file.
My Fresco code (taken from Getting Started with Fresco guide):
Uri uri = Uri.parse("http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api");
SimpleDraweeView draweeView = findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
XML code for the SimpleDraweeView:
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/my_image_view"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
fresco:placeholderImage="#drawable/search_white_24dp" />
JSON I'm working with (for more Google Play Books thumbnails): https://pastebin.com/hq4AFMRb
Please let me know if you need any more information (e.g. XML). I don't mind using a different third-party library (including Glide or Picasso) if that works.
Looks like, that unprotected traffic is no longer available for making requests to the Google Books servers (Pay attention at the piece of the Glide's log below):
08-09 22:31:06.428 7606-7606/com.sandbox.test W/Glide: Load failed for
http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api
with size [2392x2392]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There were 2 causes:
java.io.IOException(Cleartext HTTP traffic to books.google.com not permitted)
java.io.FileNotFoundException(No content provider: http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Fetching data failed,
class java.io.InputStream, REMOTE
There was 1 cause:
So, you can just add the "S" letter to pictures url protocol from the API and make a request through HTTPS.
In my case, this did the trick.

MVVMCross : How to bind ImageView to local Image (saved in SD Card)

I want to bind my ImageView control to my property URL (it's my image local path like this : /storage/emulated/0/MyFolder/IMG_20160411_094834.JPEG
I try to bind my control like this :
<ImageView
android:src="#android:drawable/ic_menu_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ImagePreview"
local:MvxBind="ImageUrl ImagePath" />
ImagePath is my property but i can't use this method.
Can you help me ?
Thank you
You have to use Mvx.MvxImageView.
Changing ImageView to Mvx.MvxImageView should fix it.
Ensure, that you have installed the
File plugin (https://www.nuget.org/packages/MvvmCross.Plugin.File/)
DownloadCache plugin (https://www.nuget.org/packages/MvvmCross.Plugin.DownloadCache/)
in your android and core project.

How do I use MvxImageView.DefaultImagePath?

I have successfully used MvxImageView with ImageUrl, but how can I set a drawable icon as a default for those users which don't have a profilepicture (that is, where ProfileThumbnailImageUrl is empty)?
<MvxImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
local:MvxBind="ImageUrl ProfileThumbnailImageUrl"
DefaultImagePath="#drawable/ic_action_user"/>
If this view is just being used once (e.g. not reused in a list) then you can just use android:src for your default image.
If you need to use DefaultImagePath then you can do this in a binding:
local:MvxBind="ImageUrl ProfileThumbnailImageUrl; DefaultImagePath DefaultImageUrl"
where DefaultImageUrl is a drawable accessed as res:name - e.g. res:ic_action_user, a file path (saved using the File plugin), or an http url
You should also be able to do this as a string literal:
local:MvxBind="ImageUrl ProfileThumbnailImageUrl; DefaultImagePath 'res:ic_action_user'"

Opening a local HTML file

I have a textview in a layout (called - t_c) with the code:-
<TextView
android:id="#+id/GoToTCContacting"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="2"
android:background="#drawable/border2"
android:clickable="true"
android:onClick="GoToTCContacting"
android:padding="10dp"
android:text="Contacting"
android:textColor="#FFF"
android:textSize="18sp" />
I want the textview to open another layout (called - t_c_contacting) on click which has a webview in and that webview to open a html file I have in my layout folder called con.html.
This is how the t_c_contacting layout is coded:-
<WebView
android:id="#+id/contactingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This is how my class is set up to open t_c_contacting which works fine but I can't work out how to populate the webview with my file.
public void GoToTCContacting(View view)
{
setContentView(R.layout.t_c_contacting);
}
If you already have the HTML stored localy, simply call:
webView.loadDataWithBaseURL(mUrl, mHtmlData, MIME_TYPE, CHARSET, "");
Where:
mUrl can be a dummy Url if you want, or the actual URL of the page, so the WebView can figure out how to place some UI elements such as Images.
mHtmlData is a String that contains the actual HTML content.
MIME_TYPE is a String that represents the mimeType of the data, say: "text/html"
CHARSET is a String that represents the encoding of the data, say: "utf-8"
I hope that helps.

Categories

Resources