Glide won't Load images from a URL and crashes the app - android

So I am having an issue where even the most barebones implementation of Glide to load an image won't work and results in a AppX has stopped. I even created a side app with just the necessary code required to load from a URL and it doesn't work. I have tried several URLs that I know work and get the same issue. I have added the permissions :
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
the gradle instructions:
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:23.0.0'
and my activity XML looks like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.benhouse.basicglidetest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="138dp"
android:layout_height="495dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:layout_editor_absoluteX="192dp"
tools:layout_editor_absoluteY="155dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
and my .java for that activity:
package com.example.benhouse.basicglidetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Glide.with(getApplicationContext()).load("https://i.imgur.com/AxETlhd.jpg");
}
}
What is going wrong? I am not getting any stack traces in the logcat or anything and I have isolated the problem as much as possible. Also is there a way to display on the app that the image didn't load?

Try this .
imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(MainActivity.this).load("https://i.imgur.com/AxETlhd.jpg").into(imageView);
Add into(imageView); to the code .
Note
Adding error image and placehoder image will be well .
Glide.with(context).load(url).placeholder(R.mipmap.placeholder).error(R.mipmap.error).into(iv);

Please Check this code:
I had changed some of the lines from your snippet:
package com.example.benhouse.basicglidetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.yourImageView); // get your image view
Glide.with(getApplicationContext())
.load("https://i.imgur.com/AxETlhd.jpg")
.into(imageView); // put image loaded via glide into ImageView
}
}

Add loaded image into imageview
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView) findViewById(R.id.imageView);
Glide.with(EnterPinActivity.this).load("https://i.imgur.com/AxETlhd.jpg").placeholder(R.drawable.plcae_holder).error(R.drawable.plcae_holder).into(imageView);
}
}

Related

Passing an image from an activity to another one

I want to pass images from one activity to another when button clicked. I have one button "Show Image" in first activity. When I click on it, it should pass two images from my mipmap folder of my project and go to second activity and show one of the passed image on the ImageView of that activity. On second activity, I have two buttons which are supposed to receive images and show those images when clicked on each button. I tried using intent to pass the image, however, it didn't work. Is there other way to send images from mipmap folder from one activity to another?
Here is my code:
MainActivity.java
package com.example.abina.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showmyImage();
}
});
}
public void showmyImage(){
Intent intent = new Intent(this, Main2Activity.class);
Bitmap bitmap; // your bitmap
bitmap = null;
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intent.putExtra("byteArray", _bs.toByteArray());
startActivity(intent);
}
}
activity_main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="137dp"
android:text="Show Image" />
</RelativeLayout>
Main2Activity.java
package com.example.abina.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class Main2Activity extends AppCompatActivity {
Button image1;
Button image2;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
image1 =(Button) findViewById(R.id.image1);
image2 = (Button) findViewById(R.id.image2);
imageView =(ImageView) findViewById(R.id.imageView);
if(getIntent().hasExtra("byteArray")) {
Bitmap _bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
imageView.setImageBitmap(_bitmap);
}
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<Button
android:id="#+id/image1"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Image1" />
<Button
android:id="#+id/image2"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="0dp"
android:text="Image2" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Images
First the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders.
Next since they will be in your drawables, I would just pass the #DrawableRes id e.g. the R.id.image_name value.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
startActivity(intent);
Also I would recommend that you use a public static variable IMAGE_RES_ID_KEY for your extra key to avoid typos.
Then on the other side you can simply
if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
}

EditText change setError text position

So so what im trying to implement has two parts
One im giving my edittext the passwordToggle for which im using
android.support.design.widget TextInputLayout+TextInputEditText
So this is how my edittext looks like
Part two is i want to add validation and set appropriate error message.
I need the error message to be shown as follows
My layout code is as follows
<android.support.design.widget.TextInputLayout
    style="#style/editTextBold"
    android:id="#+id/input_pwd_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintAnimationEnabled="false"
    app:hintEnabled="false"
    app:passwordToggleEnabled="true">
    <android.support.design.widget.TextInputEditText
        android:id="#+id/input_pwd"
        style="#style/editTextBold"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#android:color/transparent"
        android:ems="10"
        android:hint="#string/hint_pwd"
        android:inputType="textPassword"
        android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
So what i want to know is
1.How do i hide/unhide the password toggle icon in the edittext via code?
2.Also how do i make the setError message appear in place of the passwordToggle icon(once i hide it via code)
Ended up creating a custom view
The component can be found here
https://github.com/vnh1991/CustomValidatorEditText
Import the lib project as a module ,
in your layout create a container
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.v2dev.customedittextdemo.MainActivity">
<LinearLayout
android:id="#+id/llContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"></LinearLayout>
<Button
android:id="#+id/btnValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#+id/llContainer"
android:padding="10dp"
android:text="Validate" />
</RelativeLayout>
And in your activity load the component into the container
package com.v2dev.customedittextdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.v_sQr_dev.customvalidatoredittext.CustomEdittext;
public class MainActivity extends AppCompatActivity {
private LinearLayout llContainer;
private Button btnValidate;
private CustomEdittext inputPwd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llContainer = (LinearLayout) findViewById(R.id.llContainer);
btnValidate = (Button) findViewById(R.id.btnValidate);
inputPwd = new CustomEdittext(llContainer, this);
inputPwd.setHint("PASSWORD");
btnValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(inputPwd.validate()){
Toast.makeText(MainActivity.this,"Input Valid",Toast.LENGTH_SHORT).show();
}
}
});
}
}

button onclick from a layout file

so I was working on some basics of android when I stumbled upon this. I am trying to implement a listview(having its layout stored in file eachrow.XML)and a button on my app such that when the user clicks one button "schoollife", the listview automatically imports layout stored from other file "eachrow2.XML".
the layout of eachrow.xml consists of buttons only, while the layout of eachrow2.xml consists of buttons.
Everything seems to work fine, the user clicking the button imports the other layout , but the problem starts when user tries to click buttons from the second layout. in the error logs, it is stating that the button 'subject' causing a null pointer exception
eachrow.xml:
eachrow2.xml:
===========codes========
eachrow2.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/subjectbutton"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="1dp"
android:layout_margin="1dp"
android:textAllCaps="false"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/marksbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="click the button to show marks of each subject"
android:textAlignment="center"
/>
</LinearLayout>
mainactivity.java:
package com.example.ansh.reportcard;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ListView l;
private String[] d_myself;
private ArrayAdapter adp;
private HashMap<String,String> d_school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.a_list);
d_myself= new String[]{"something"
};
d_school=new HashMap<>();
d_school.put("A","B");
Button B_myself= (Button)findViewById(R.id.myself);
B_myself.setOnClickListener(this);
Button B_school=(Button)findViewById(R.id.school);
B_school.setOnClickListener(this);
Button subject=(Button)findViewById(R.id.subjectbutton);
subject.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.myself){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow,R.id.textView,d_myself);
l.setAdapter(adp);
}
if (view.getId()==R.id.school){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow2,R.id.subjectbutton,d_school.keySet().toArray());
l.setAdapter(adp);
}
if (view.getId()==R.id.subjectbutton){
Button tmp=(Button)view;
CharSequence x=tmp.getText();
TextView t= (TextView) findViewById(R.id.marksbox);
t.setText(d_school.get(x.toString()));
}
}
}
According to my understanding to your question, You declaring the Button from the xml file activity_main.xml, Instead you should find it from the eachrow2.xml file.
The subjectbutton is in the eachrow2.xml so you need to find the view from this file only instead activity_main.xml.

Android custom Imageview throws 'can't cast' exception

I'd like to use the ImageViewTopCrop class described in this question:
ImageView scaling TOP_CROP
My custom view is based on this code:
Custom Image View class
Here is a layout file using this custom view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_background"
android:background="#000"
android:orientation="vertical">
<FrameLayout
android:id="#+id/details_frame"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6">
<com.grayraven.imagetest.ImageViewTopCrop
android:id="#+id/grid_item_image"
android:src="#drawable/hobbit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
Here's how I'm trying to display it in code:
com.grayraven.imagetest;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class DetailsActivity extends ActionBarActivity {
private TextView titleTextView;
private com.grayraven.imagetest.ImageViewTopCrop imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_view);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
String title = getIntent().getStringExtra("title");
String imageUrl = getIntent().getStringExtra("imageUrl");
titleTextView = (TextView) findViewById(R.id.title);
imageView = (com.grayraven.project1.ImageViewTopCrop) findViewById(R.id.grid_item_image);
//Exception thrown here:
//android.widget.ImageView cannot be cast to com.grayraven.imagetest.ImageViewTopCrop
//can't go any further
}
}
What simple thing am I missing?
I think you are type casting with wrong class.
You used
com.grayraven.imagetest.ImageViewTopCrop
in your xml but type casting with com.grayraven.project1.ImageViewTopCrop
I was missing a constructor in my custom view class. Never mind.

Android: Unable to display thumbnail of video

I am trying to display thumbnail of a video but I am not able to generate the thumbnail for the video.
My activity_main contains
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/Thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My Main Activity has the following code
package com.example.video1;
import android.app.Activity;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore.Video.Thumbnails;
import android.widget.ImageView;
public class MainActivity extends Activity {
String filePath = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView thumbnail_mini = (ImageView) findViewById(R.id.Thumbnail);
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(filePath,
Thumbnails.MINI_KIND);
thumbnail_mini.setImageBitmap(bmThumbnail);
}
}
Its displaying nothing . How can I display the thumbnail of video?
You can't get the thumbnail from a link with the android SDK. You would have to download it locally and then use the thumbnail.utils

Categories

Resources