在Android中的图像适配器本地保存图像

时间:2015-07-07 11:26:01

标签: android android-contentprovider android-adapter android-lifecycle picasso

这是关于Android应用中图像保存的问题。 我正在努力制作一个udacity project.Link - https://docs.google.com/document/d/1gtXUu1nzLGWrGfVCD6tEA0YHoYA9UNyT2yByqjJemp8/pub?embedded=true

的热门电影应用

我在ImageAdapter中有用于图像下载的毕加索库 -
这是我的ImageAdapter代码 -

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

现在我有两个问题 -

  1. 当我从其他活动返回主要活动时,会显示本地图像,而不是先前下载图像。
  2. 当我关闭我的应用程序并再次打开时,我会看到本地图像显示而不是上次下载的图像。有人告诉你,在使用文件或内容提供商下载到本地内存后,你必须保存这些图像。但我不知道该怎么办?
  3. 3.当我改变设备方向时,我面临同样的问题。

    这是我主要活动的片段 -

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

    寻求帮助因为我是android的新手。

2 个答案:

答案 0 :(得分:1)

  

当我关闭我的应用并再次打开时,我会看到本地图像显示而不是上次下载的图像。有人告诉你,在使用文件或内容提供商下载到本地内存后,你必须保存这些图像。但我不知道该怎么办?

这是预料之中的,因为您的设备上没有任何照片。将图像保存在数据库中(SQLite)如果您希望它在您返回应用程序时保持不变并可用。或者每次重新启动活动时都加载它们,最好是通过AsyncTask。

要在SQLite数据库中存储图像,请查看How to store(bitmap image) and retrieve image from sqlite database in android?告诉您有关保存和检索图像的所有信息。

答案 1 :(得分:0)

试试这个,

<强>答案-1:

尝试将适配器初始化逻辑更改为onCreateView为onCreate。 把这一行放在onCreate上。

imag_adap = new ImageAdapter(getActivity());

<强>答案-2:

下载图像后,将uri路径保存在数据库或文件或共享首选项中。然后在创建适配器中检索uri。

ArrayList<String> uriPathsFromDb=getListOfUriFromDatabase();
if(uriPathsFromDb.size()==0){
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;
}else{
   mThumbUris=uriPathsFromDb;
}