本文介绍了在Android Studio中加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的数据库中显示ImageView中的图像...但我失败了...并且没有显示错误...

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

这是我的MainFragment(按Detailtxt工作正常的方式..)

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();
        }
    }

}

这是我的fragment_main.xml

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>

这是我在Imageloader.java中的DisplayImage

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);
        }

你可以看到stub_id = R. drawable.gg;..并且在fragment_main.xml中显示的图像是来自drawable的gg.jpg .....
就像bitmapp结果为null ..我不知道如何解决这个问题.. 。

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...

推荐答案

您可以使用名为显示图像。

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

首先在 build.gradle 文件中添加gradle依赖项

First add the gradle dependency in your build.gradle file

编译'com.squareup.picasso:picasso:2.5.2'

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

添加后,build.gradle文件可能如下所示

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'
}

然后代替这个

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

                            detailtxt.setText(edetail);
                        }
                    });

使用此

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

编辑

如果你不想缓存

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

这篇关于在Android Studio中加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:33