在本地保存图像在Android的图像适配器 [英] Save Images locally in Image adapter in android

查看:200
本文介绍了在本地保存图像在Android的图像适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于Android应用图像保存的问题。
我试图使一个udacity project.Link的热门电影的应用程序 - <一个href=\"https://docs.google.com/document/d/1gtXUu1nzLGWrGfVCD6tEA0YHoYA9UNyT2yByqjJemp8/pub?embedded=true\" rel=\"nofollow\">https://docs.google.com/document/d/1gtXUu1nzLGWrGfVCD6tEA0YHoYA9UNyT2yByqjJemp8/pub?embedded=true

我对毕加索的图像库中下载结果ImageAdapter--
这里是我的ImageAdapter code -

 包jindal5.mayank.popular_movies_14ce10032_gsc;公共类ImageAdapter延伸BaseAdapter {
    私人字符串绘制preFIX;
    私人语境mContext;
    私人的ArrayList&LT;串GT; mThumbUris;
    私人的ArrayList&LT;串GT; mov_id_arr;
    私人的ArrayList&LT;串GT; mov_tit_arr;    公共ImageAdapter(上下文C){
        mContext = C;
        串PACKNAME = mContext.getPackageName();
        绘制preFIX =android.resource://+ PACKNAME +/;
        ArrayList的&LT;串GT; mov_id_arr =新的ArrayList&LT;&GT;();
        ArrayList的&LT;串GT; mov_tit_arr =新的ArrayList&LT;&GT;();
        mov_tit_arr.add(ASDF);
        mov_tit_arr.add(ASDF);
        mov_tit_arr.add(ASDF);
        mov_tit_arr.add(ASDF);
        mov_tit_arr.add(ASDF);
        mov_tit_arr.add(ASDF);        ArrayList的&LT;串GT; uriPaths =新的ArrayList&LT;&GT;(); //将您可绘制。        uriPaths.add(绘preFIX + R.drawable.sample_0);
        uriPaths.add(绘preFIX + R.drawable.sample_0);
        uriPaths.add(绘preFIX + R.drawable.sample_0);
        uriPaths.add(绘preFIX + R.drawable.sample_0);
        uriPaths.add(绘preFIX + R.drawable.sample_0);
        uriPaths.add(绘preFIX + R.drawable.sample_0);
        mThumbUris = uriPaths;
    }    公众诠释的getCount(){
        返回mThumbUris.size();
    }    公共对象的getItem(INT位置){
        返回mThumbUris.get(位置);
    }    众长getItemId(INT位置){
        返回0;
    }    //创建由适配器引用的每个项目的新的ImageView
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        ImageView的ImageView的;
        如果(convertView == NULL){
            //如果它不回收,初始化一些属性
            ImageView的=新ImageView的(mContext);
           // imageView.setLayoutParams(新GridView.LayoutParams(85,85));
           // imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
           // imageView.setPadding(8,8,8,8);
        }其他{
            ImageView的=(ImageView的)convertView;
        }
        乌里imgUri = Uri.parse(mThumbUris.get(位置));        Picasso.with(mContext)
                .load(imgUri)
                .placeholder(R.drawable.sample_0)
               .centerCrop()
                .resize(400,400)
                .into(ImageView的);
       imageView.setImageResource(mThumbIds [位置]);
       // imageView.setAdjustViewBounds(真);
        返回ImageView的;
    }    公众的ArrayList&LT;串GT; getUriList(){
        返回mThumbUris;
    }
    公众的ArrayList&LT;串GT; getidlist(){
        返回mov_id_arr;
    }
    公众的ArrayList&LT;串GT; gettitlist(){
        返回mov_tit_arr;
    }    //我们的图像的引用
    私人整数[] = mThumbIds {
            R.drawable.sample_2,R.drawable.sample_3,
            R.drawable.sample_4,R.drawable.sample_5,
            R.drawable.sample_6,R.drawable.sample_7,
            R.drawable.sample_0,R.drawable.sample_1,
            R.drawable.sample_2,R.drawable.sample_3,
            R.drawable.sample_4,R.drawable.sample_5,
            R.drawable.sample_6,R.drawable.sample_7,
            R.drawable.sample_0,R.drawable.sample_1,
            R.drawable.sample_2,R.drawable.sample_3,
            R.drawable.sample_4,R.drawable.sample_5,
            R.drawable.sample_6,R.drawable.sample_7
    };
}

现在我有两个问题 -


  1. 当我从另一个活动返回到主要活动,当地的图像出现,而不是previously下载图像。

  2. 当我关闭应用程序,我再次打开看到本地的图像出现,而不是在上一次下载的图像。有人告诉你要保存使用文件或内容提供商在本地内存下载后的图像。但我不知道该怎么做?

3,当我改变了设备的方向我面临同样的问题。

这是我的主要活动的片段 -

 包jindal5.mayank.popular_movies_14ce10032_gsc;/ **
 *包含一个简单视图中的占位符片段。
 * /
公共类MainActivityFragment扩展片段{
    公共ImageAdapter imag_adap;
    公共字符串movieJsonStr;
    公众的ArrayList&LT;串GT; tit_arr =新的ArrayList&LT;串GT;();
    公众的ArrayList&LT;串GT; rel_dat_arr =新的ArrayList&LT;串GT;();
    公众的ArrayList&LT;串GT; url1_arr =新的ArrayList&LT;串GT;();
    公众的ArrayList&LT;串GT; over_arr =新的ArrayList&LT;串GT;();
    公众的ArrayList&LT;串GT; id_arr =新的ArrayList&LT;串GT;();
    公共字符串put_ext_tit;
    公共字符串rel_date_ext;
 公共字符串url_for_pos_ex;
    公共字符串over_ex;
    公共字符串id_ex;
    公共MainActivityFragment(){
    }
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        //为了添加这一行这个片段来处理菜单事件。
        setHasOptionsMenu(真);
    }    @覆盖
    公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
        inflater.inflate(R.menu.menu_fragment,菜单);
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();
        如果(ID == R.id.action_refresh){
            //共享preferences共享= preferenceManager.getDefaultShared preferences();
          //字符串为了= share.getString()
            的getImage imageTask =新的getImage();
            imageTask.execute(popularity.desc);
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
       查看rootview = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);
        GridView的网格=(GridView控件)rootview.findViewById(R.id.gridView);
        imag_adap =新ImageAdapter(getActivity());
        grid.setAdapter(imag_adap);        grid.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图&LT;&GT;适配器视图,视图观点,INT I,长L){
                的String [] = TIT1 tit_arr.toArray(新的String [tit_arr.size()]);
                的String [] = REL_DATE rel_dat_arr.toArray(新的String [rel_dat_arr.size()]);
                的String [] = url_for_pos url1_arr.toArray(新的String [url1_arr.size()]);
                的String [] =超过url1_arr.toArray(新的String [over_arr.size()]);
                的String [] n = id_arr.toArray(新的String [over_arr.size()]);
               // Log.v(mayank,TIT1 [3]);
                尝试{
                    put_ext_tit = TIT1 [I]
                    rel_date_ext = REL_DATE [I]
                    url_for_pos_ex = url_for_pos [I]
                    over_ex =超过[I]
                    id_ex = ID [I]
                    意向意图=新意图(getActivity(),det_mov.class);
                    //.putExtra(Intent.EXTRA_TEXT,put_ext_tit);
                    捆绑额外=新包();
                    extras.putString(称号,put_ext_tit);
                    extras.putString(REL_DATE,rel_date_ext);
                    extras.putString(url_pos,url_for_pos_ex);
                    extras.putString(over_ex,over_ex);
                    extras.putString(id_ex,id_ex);
                    extras.putInt(断定一);
                    intent.putExtras(临时演员);
                    startActivity(意向);
                }
                赶上(ArrayIndexOutOfBoundsException异常P){
                    Log.e(mayank,章,p)的;
                }
            }
        });        返回rootview;
    }    公共类的getImage扩展的AsyncTask&LT;字符串,字符串,字符串[]&GT; {
        ConnectivityManager conMgr =(ConnectivityManager)getActivity()getSystemService(Context.CONNECTIVITY_SERVICE)。
        私人最终字符串LOG_TAG = getimage.class.getSimpleName();
        公众诠释计数;
        的String [] pos_path;
        的String [] mov_id;
        的String []针锋相对;
        的String []转;
        的String [] rel_dat;
        的String []为url1;
        的String []过;
        的String [] ID;
        公共GetAd出现ImageAdapter(){
            返回imag_adap;
        }
        @覆盖
        受保护的String [] doInBackground(字符串... PARAMS){
            HttpURLConnection类的URLConnection = NULL;            读者的BufferedReader = NULL;
            尝试{
                最终字符串movie_url_str =htt​​p://api.themoviedb.org/3/discover/movie?sort_by=​​\"+ PARAMS [0] +与&amp; API_KEY = 8d7a48043ba1d3348181e2b6615cedc7;
                网址movie_url =新的URL(movie_url_str);
                URLConnection的=(HttpURLConnection类)movie_url.openConnection();
                urlConnection.setRequestMethod(GET);
                urlConnection.connect();
                为InputStream的InputStream = urlConnection.getInputStream();
                StringBuffer的缓冲区=新的StringBuffer();
                如果(InputStream的== NULL){
                    返回null;
                }
                读者=新的BufferedReader(新的InputStreamReader(InputStream的));
                串线;
                而((行= reader.readLine())!= NULL){
                    buffer.append(行+\\ n);
                }
                如果(buffer.length()== 0){
                    返回null;
                }
               movieJsonStr = buffer.toString();                JSONObject的movieJson =新的JSONObject(movieJsonStr);
                JSONArray movieJsonarray = movieJson.getJSONArray(结果);
                 数= movieJsonarray.length();
                pos_path =新的String [统计]
                mov_id =新的String [统计]
                针锋相对=新的String [统计]
                REV =新的String [统计]
                rel_dat =新的String [统计]
                为url1 =新的String [统计]
                超过=新的String [统计]
                ID =新的String [统计]
                的for(int i = 0; I&LT;计数;我++){
                    JSONObject的sin_movie = movieJsonarray.getJSONObject(I)
                    pos_path [I] = sin_movie.getString(poster_path);
                    在[I] = sin_movie.getString(概览);
                    Uri.Builder url_for_poster =新Uri.Builder();
                    字符串QW =T;
                    字符串=P;
                    Uri.Builder url_build = url_for_poster.scheme(\"http\").authority(\"image.tmdb.org\").appendPath(qw).appendPath(as).appendPath(\"w500\").appendEn$c$cdPath(pos_path[i]);
                     为url1 [I] = url_build.toString();                   // mov_id [I] = sin_movie.getString(ID);
                   针锋相对[I] = sin_movie.getString(标题);
                    //转[I] = sin_movie.getString(回顾);
                    rel_dat [I] = sin_movie.getString(RELEASE_DATE);
                    编号[I] = sin_movie.getString(ID);
                    publishProgress((String)(tit[i]),(String)(rel_dat[i]),(String)(url1[i]),(String)(over[i]),(String)(id[i]));
                    Log.v(LOG_TAG,预测字符串:+ ID [I]);                }
            }赶上(IOException异常| JSONException E){
                Log.e(LOG_TAG,错误,E);
                返回null;
            }
            最后{
                如果(URLConnection的!= NULL){
                    urlConnection.disconnect();
                }
                如果(读者!= NULL){
                    尝试{
                        reader.close();
                    }赶上(最终IOException异常五){
                        Log.e(LOG_TAG,错误收盘流,E);
                    }
                }
            }
            返回pos_path;
            }
        @覆盖
        保护无效onProgressUpdate(字符串值...){                tit_arr.add(值[0]);
            rel_dat_arr.add(值[1]);
            url1_arr.add(值[2]);
            over_arr.add(值[3]);
            id_arr.add(值[4]);
        }        @覆盖
        保护无效onPostExecute(的String []的结果){            ArrayList的&LT;串GT; uriPaths = imag_adap.getUriList();
            uriPaths.clear();
           // ImageView的[] im_vi =新ImageView的[统计]            对于(INT J = 0; J&LT;计数; J ++){               字符串pos_sin_path = pos_path [J]。
                字符串id_mov = ID [J]。
                Uri.Builder url_for_poster =新Uri.Builder();
                Uri.Builder url_for_rev =新Uri.Builder();
                字符串QW =T;
                字符串=P;
              Uri.Builder url_build = url_for_poster.scheme(\"http\").authority(\"image.tmdb.org\").appendPath(qw).appendPath(as).appendPath(\"w500\").appendEn$c$cdPath(pos_sin_path);
                字符串URL = url_build.toString();
                Uri.Builder url_build_rev = url_for_rev.scheme(\"http\").en$c$cdAuthority(\"api.themoviedb.org/3/movie\").appendPath(id_mov).appendPath(\"reviews\");
                uriPaths.add(URL);
               // mov_title.add(针锋相对[J]​​);
                //mov_id_arr.add(id);
             // mov_tit_arr.add(职称);
             //}
              //赶上(UnsupportedEncodingException五){              //}
                //url_build.clearQuery();                //Picasso.with(getActivity()).load(\"http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg\").into(im_vi[j]);
                //imag_adap.getItem(j)= im_vi [J]。\\            }
            imag_adap.notifyDataSetChanged();
        }
    }
}

寻找一个帮助我作为android系统中新的。


解决方案

  

当我结束我的应用程序,我再次打开看到本地的图像出现,而不是在上一次下载的图像。有人告诉你要保存使用文件或内容提供商在本地内存下载后的图像。但我不知道该怎么做?


当您返回到您的应用程序这是意料之中的,因为你没有在照片上的任何地方device.Save在数据库中(SQLite的)图像如果你想将它保存和使用。或者你每次重新启动你的活动,preferably通过AsyncTask的时间加载它们。

要存储图像在SQLite数据库,看看<一个href=\"http://stackoverflow.com/questions/11790104/how-to-storebitmap-image-and-retrieve-image-from-sqlite-database-in-android\">How从SQLite数据库存储(位图图像​​),并获取图像中的Andr​​oid?告诉你需要知道的关于保存和检索图像的一切。

This is a question about image saving in android app. I am trying to make popular movies app of an udacity project.Link - https://docs.google.com/document/d/1gtXUu1nzLGWrGfVCD6tEA0YHoYA9UNyT2yByqjJemp8/pub?embedded=true

I have picasso library for image downloading in ImageAdapter--
Here is my ImageAdapter code -

package jindal5.mayank.popular_movies_14ce10032_gsc;

public class ImageAdapter extends BaseAdapter {
    private String drawablePrefix;
    private Context mContext;
    private ArrayList<String> mThumbUris;
    private ArrayList<String> mov_id_arr;
    private ArrayList<String> mov_tit_arr;

    public ImageAdapter(Context c) {
        mContext = c;
        String packName=mContext.getPackageName();
        drawablePrefix="android.resource://" +packName+ "/";
        ArrayList<String> mov_id_arr =new ArrayList<>();
        ArrayList<String> mov_tit_arr=new ArrayList<>();
        mov_tit_arr.add("asdf");
        mov_tit_arr.add("asdf");
        mov_tit_arr.add("asdf");
        mov_tit_arr.add("asdf");
        mov_tit_arr.add("asdf");
        mov_tit_arr.add("asdf");

        ArrayList<String> uriPaths=new ArrayList<>();// place your drawables.

        uriPaths.add(drawablePrefix+ R.drawable.sample_0);
        uriPaths.add(drawablePrefix+ R.drawable.sample_0);
        uriPaths.add(drawablePrefix+ R.drawable.sample_0);
        uriPaths.add(drawablePrefix+ R.drawable.sample_0);
        uriPaths.add(drawablePrefix+ R.drawable.sample_0);
        uriPaths.add(drawablePrefix+ R.drawable.sample_0);


        mThumbUris=uriPaths;
    }

    public int getCount() {
        return mThumbUris.size();
    }

    public Object getItem(int position) {
        return mThumbUris.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
           // imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
           // imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
           // imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        Uri imgUri=Uri.parse(mThumbUris.get(position));

        Picasso.with(mContext)
                .load(imgUri)
                .placeholder(R.drawable.sample_0)
               .centerCrop()
                .resize(400,400)
                .into(imageView);
       imageView.setImageResource(mThumbIds[position]);
       // imageView.setAdjustViewBounds(true);
        return imageView;
    }

    public ArrayList<String> getUriList(){
        return mThumbUris;
    }
    public ArrayList<String> getidlist(){
        return mov_id_arr;
    }
    public ArrayList<String> gettitlist(){
        return mov_tit_arr;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

Now I have two problems -

  1. When I return back to Main Activity from another activity ,local images appear rather than previously downloading images.
  2. When I close my app and open again I see the local images appear rather than downloaded images at last time. Somebody told that you have to save that images after downloading in local memory using File or content provider. But I don't know how to do that ?? .

3.When I change my device orientation I face the same problem.

This is my fragment of main activity-

package jindal5.mayank.popular_movies_14ce10032_gsc;

/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {
    public ImageAdapter imag_adap;
    public String movieJsonStr;
    public ArrayList<String > tit_arr = new ArrayList<String>();
    public ArrayList<String > rel_dat_arr = new ArrayList<String>();
    public ArrayList<String > url1_arr = new ArrayList<String>();
    public ArrayList<String > over_arr = new ArrayList<String>();
    public ArrayList<String > id_arr = new ArrayList<String>();
    public String put_ext_tit;
    public String rel_date_ext;
 public  String url_for_pos_ex;
    public String over_ex;
    public String id_ex;
    public MainActivityFragment() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add this line in order for this fragment to handle menu events.
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_fragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_refresh) {
            //SharedPreferences share = PreferenceManager.getDefaultSharedPreferences();
          //  String order = share.getString()
            getimage imageTask = new getimage();
            imageTask.execute("popularity.desc");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View rootview = inflater.inflate(R.layout.fragment_main, container, false);
        GridView grid = (GridView) rootview.findViewById(R.id.gridView);
        imag_adap = new ImageAdapter(getActivity());
        grid.setAdapter(imag_adap);

        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String[] tit1 = tit_arr.toArray(new String[tit_arr.size()]);
                String[] rel_date = rel_dat_arr.toArray(new String[rel_dat_arr.size()]);
                String[] url_for_pos = url1_arr.toArray(new String[url1_arr.size()]);
                String[] over = url1_arr.toArray(new String[over_arr.size()]);
                String[] id = id_arr.toArray(new String[over_arr.size()]);
               // Log.v("mayank",tit1[3]);
                try{
                    put_ext_tit = tit1[i];
                    rel_date_ext = rel_date[i];
                    url_for_pos_ex = url_for_pos[i];
                    over_ex = over[i];
                    id_ex =id[i];
                    Intent intent = new Intent(getActivity(),det_mov.class);
                    //.putExtra(Intent.EXTRA_TEXT,put_ext_tit);
                    Bundle extras = new Bundle();
                    extras.putString("title",put_ext_tit);
                    extras.putString("rel_date",rel_date_ext);
                    extras.putString("url_pos",url_for_pos_ex);
                    extras.putString("over_ex",over_ex);
                    extras.putString("id_ex", id_ex);
                    extras.putInt("posit",i);
                    intent.putExtras(extras);
                    startActivity(intent);
                }
                catch (ArrayIndexOutOfBoundsException p){
                    Log.e("mayank","reg",p);
                }
            }
        });

        return rootview;
    }

    public class getimage extends AsyncTask<String, String, String[]> {
        ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


        private final String LOG_TAG = getimage.class.getSimpleName();
        public int count;
        String[] pos_path;
        String[]  mov_id;
        String[] tit;
        String[] rev;
        String[] rel_dat;
        String[] url1;
        String[] over;
        String[] id;
        public ImageAdapter getad(){
            return imag_adap;
        }


        @Override
        protected String[] doInBackground(String... params) {
            HttpURLConnection urlConnection = null;

            BufferedReader reader = null;
            try {
                final String movie_url_str = "http://api.themoviedb.org/3/discover/movie?sort_by="+ params[0] +"&api_key=8d7a48043ba1d3348181e2b6615cedc7";
                URL movie_url = new URL(movie_url_str);
                urlConnection = (HttpURLConnection) movie_url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                    return null;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n");
                }
                if (buffer.length() == 0) {
                    return null;
                }
               movieJsonStr = buffer.toString();

                JSONObject movieJson = new JSONObject(movieJsonStr);
                JSONArray movieJsonarray = movieJson.getJSONArray("results");
                 count = movieJsonarray.length();
                pos_path = new String[count];
                mov_id = new String[count];
                tit = new String[count];
                rev = new String[count];
                rel_dat = new String[count];
                url1 = new String[count];
                over = new String[count];
                id = new String[count];
                for(int i=0;i<count;i++){
                    JSONObject sin_movie = movieJsonarray.getJSONObject(i);
                    pos_path[i] = sin_movie.getString("poster_path");
                    over[i] = sin_movie.getString("overview");
                    Uri.Builder url_for_poster = new Uri.Builder();
                    String qw = "t";
                    String as = "p";
                    Uri.Builder url_build =  url_for_poster.scheme("http").authority("image.tmdb.org").appendPath(qw).appendPath(as).appendPath("w500").appendEncodedPath(pos_path[i]);
                     url1[i] = url_build.toString();

                   // mov_id[i] =  sin_movie.getString("id");
                   tit[i] = sin_movie.getString("title");
                    //rev[i] = sin_movie.getString("review");
                    rel_dat[i] = sin_movie.getString("release_date");
                    id[i] = sin_movie.getString("id");
                    publishProgress((String)(tit[i]),(String)(rel_dat[i]),(String)(url1[i]),(String)(over[i]),(String)(id[i]));
                    Log.v(LOG_TAG, "Forecast string: " + id[i]);

                }
            } catch (IOException | JSONException e) {
                Log.e(LOG_TAG, "Error ", e);
                return null;
            }
            finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e(LOG_TAG, "Error closing stream", e);
                    }
                }
            }
            return pos_path;
            }
        @Override
        protected void onProgressUpdate(String... values) {

                tit_arr.add(values[0]);
            rel_dat_arr.add(values[1]);
            url1_arr.add(values[2]);
            over_arr.add(values[3]);
            id_arr.add(values[4]);
        }

        @Override
        protected void onPostExecute(String[] result) {

            ArrayList<String> uriPaths = imag_adap.getUriList();
            uriPaths.clear();
           // ImageView[] im_vi = new ImageView[count];

            for(int j=0;j<count;j++) {

               String  pos_sin_path = pos_path[j];
                String id_mov = id[j];
                Uri.Builder url_for_poster = new Uri.Builder();
                Uri.Builder url_for_rev = new Uri.Builder();
                String qw = "t";
                String as = "p";
              Uri.Builder url_build =  url_for_poster.scheme("http").authority("image.tmdb.org").appendPath(qw).appendPath(as).appendPath("w500").appendEncodedPath(pos_sin_path);
                String url = url_build.toString();
                Uri.Builder url_build_rev = url_for_rev.scheme("http").encodedAuthority("api.themoviedb.org/3/movie").appendPath(id_mov).appendPath("reviews");
                uriPaths.add(url);
               // mov_title.add(tit[j]);
                //mov_id_arr.add(id);
             //   mov_tit_arr.add(title);
             // }
              //catch (UnsupportedEncodingException e){

              //}
                //url_build.clearQuery();

                //Picasso.with(getActivity()).load("http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg").into(im_vi[j]);
                //imag_adap.getItem(j) = im_vi[j];\

            }
            imag_adap.notifyDataSetChanged();
        }
    }
}

Looking for a help As I am new in android.

解决方案

When I close my app and open again I see the local images appear rather than downloaded images at last time. Somebody told that you have to save that images after downloading in local memory using File or content provider. But I don't know how to do that ?? .

This is expected since you do not have the photos anywhere on your device.Save the image in a database (SQLite) If you want it persisted and available when you return to your app. Or you load them each time you restart your activity, preferably through an AsyncTask.

To store images in the SQLite database, take a look at How to store(bitmap image) and retrieve image from sqlite database in android? Tells you everything you need to know about saving and retrieving images.

这篇关于在本地保存图像在Android的图像适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆