0

画像をキャッシュに保存してから取得しています。

いくつかの URL 文字列を SharedPreferences に保存し、アクティビティが Create() になったらそれらを取り出します。

問題は、このメソッドを実行するときです..

public void getImagesfromCache(){
        ImageAdapter adapter = new ImageAdapter();

    String[] cachImages = myRemoteImages; //I set the CacheImages String[] here

    try {   
     URL aURL2 = null;
     aURL2 = new URL(cachImages[position]);  //I get a MalformedURLException here 
     URI imageCacheUri = null;


     try {
            imageCacheUri = aURL2.toURI();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(new File(new File(this.getApplicationContext().getCacheDir(), "thumbnails"),"" + imageCacheUri.hashCode()).exists()){
            Log.v("FOUND", "Images found in cache! Now being loaded!");
            String cacheFile = this.getApplicationContext().getCacheDir() +"/thumbnails/"+ imageCacheUri.hashCode();
            ImageView i = new ImageView(this.getApplicationContext());
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(cacheFile);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Bitmap bm = BitmapFactory.decodeStream(fis);

             i.setImageBitmap(bm);
             putBitmapInDiskCache(imageCacheUri, bm);

             Log.v("Loader", "Image saved to cache");

             ((Gallery) findViewById(R.id.gallery))
              .setAdapter(new ImageAdapter(MainMenu.this));
        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

アクティビティが作成されたら、各 URL を sharedpreferences の変数に設定します。

        SharedPreferences app_preferences = 
            this.getSharedPreferences("images_urls", MODE_WORLD_READABLE);
    imageUrl = app_preferences.getString("URL1", "Does not exist in preference");
    imageUrl2 = app_preferences.getString("URL2", "Does not exist in cache");
    imageUrl3 = app_preferences.getString("URL3", "Does not exist in preference");
    imageUrl4 = app_preferences.getString("URL4", "Does not exist in preference");

    Log.e("Preference", imageUrl + imageUrl2 + imageUrl3 + imageUrl4);

ご覧のとおり、ログに記録して、何かを引っ張っていることを確認します。最初に保存したすべての URL でデバッグに表示されます。

文字列が SharePreferences から取得されると、oncreate 内で getimagesfromCache() を呼び出します。

では、なぜその行で malformedURLException が発生するのでしょうか?

編集:これは私がリモート画像に使用しているコードです....

そして位置..

 int position;

public String [] myRemoteImages = {imageUrl,imageUrl2,imageUrl3,imageUrl4};

私はイメージ アダプターを持っています...これを BaseAdapter 内で使用して位置を設定します

    public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(this.myContext);

                try {

            URL aURL = new URL(myRemoteImages[position]);

編集: imageAdapter の getView() 内で int 位置を使用するにはどうすればよいですか? または私はそれが必要ですか?

4

0 に答える 0