Load Image in Android Studio

旧巷老猫 提交于 2019-12-13 04:14:15

问题


I want to show image in ImageView from my database... but I was failed.. and no error showing...

this is my MainFragment (By the way Detailtxt is working normally..)

public class MainFragment extends Fragment {

    ProgressDialog pDialog;
    JSONArray str_json = null;
    private static final String TAG_URL = "http://192.168.1.118/symanlub/readpromo.php";
    public static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    JSONParser jsonParser = new JSONParser();

    TextView detailtxt;
    ImageView img;

    View rootview;

    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootview = inflater.inflate(R.layout.fragment_main, container, false);
        new Bukaprofil().execute();
        return rootview;
    }

    class Bukaprofil extends AsyncTask<String, String, String>
    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Sedang menampilkan...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg) {

            new Thread(new Runnable() {
                @Override
                public void run() {

                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    Log.d("request!", "starting");

                    JSONObject json = jsonParser.makeHttpRequest(
                            TAG_URL, "GET", params);

                    // checking  log for json response
                    Log.d("Proses ambil data", json.toString());

                    try {
                        str_json = json.getJSONArray("hasil");

                        JSONObject ar = str_json.getJSONObject(0);

                        //fetch data
                        final String edetail = ar.getString("detail");
                        final String egambar = ar.getString("img");

                        //label
                        detailtxt = (TextView)rootview.findViewById(R.id.detailtxt);
                        //gambar
                        img = (ImageView)rootview.findViewById(R.id.gambar);


                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //gambar
                                ImageLoader imgLoader = new ImageLoader(getActivity());
                                imgLoader.DisplayImage(egambar, img);

                                detailtxt.setText(edetail);
                            }
                        });

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            pDialog.dismiss();
        }
    }

}

this is my fragment_main.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="vertical"
android:background="#fff">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="15dp">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/gambar"
                    android:layout_width="match_parent"
                    android:layout_height="450dip"
                    android:src="@drawable/pertamina222"
                    android:background="#fff"/>

            </LinearLayout>


        </LinearLayout>

        <TextView
            android:id="@+id/detailtxt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Test"
            android:layout_marginBottom="5dp"
            android:textStyle="bold"
            android:textSize="16sp"/>

    </LinearLayout>
</ScrollView>

</LinearLayout>

and this is my DisplayImage in Imageloader.java

final int stub_id=R.drawable.gg;
    public void DisplayImage(String url, ImageView imageView)
    {
        imageViews.put(imageView, url);
        Bitmap bitmap=memoryCache.get(url);
        if(bitmap!=null)
            imageView.setImageBitmap(bitmap);
        else
        {
            queuePhoto(url, imageView);
            imageView.setImageResource(stub_id);
        }

you can see "stub_id=R.drawable.gg;".. and the image that show in fragment_main.xml is gg.jpg from drawable..... is like bitmapp result is null.. I dont't know how to fix that...

please help me!! Thanks before and sorry for my bad english...


回答1:


You can use a third party library named Picasso to display image .

First add the gradle dependency in your build.gradle file

compile 'com.squareup.picasso:picasso:2.5.2'

After adding , your build.gradle file may look like this

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
}

Then instead of this

getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //gambar
                            ImageLoader imgLoader = new ImageLoader(getActivity());
                            imgLoader.DisplayImage(egambar, img);

                            detailtxt.setText(edetail);
                        }
                    });

Use this

getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //gambar
       Picasso.with(getActivity()).load(egambar).into(img);
                        }
                    });

EDIT

If you do not want cache

   getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //gambar
                              Picasso.with(getActivity())
                                .load(egambar)
                                .memoryPolicy(MemoryPolicy.NO_CACHE)
                                .into(img);
                            }
                        });


来源:https://stackoverflow.com/questions/38213832/load-image-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!