1

私は3つの断片を持っています。これらのフラグメントは、それらのフラグメント間でスワイプするために、TabsAdapter に配置されます。

問題は、アプリが読み込まれ、fragmentA ビューが作成されると、画像がダウンロードされ、その後、imageview が変更されることです。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    this.myFragmentView = inflater.inflate(R.layout.foto_setmana, container, false); //Això conté els "edittext i altres"
        new DownloadImageTask(myFragmentView).execute("http://192.168.1.35/testing/fotos/foto1.jpg");
    }
}

DownloadImageTask 内のコード:

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
View myfragmentview;

public DownloadImageTask(View myfragmentview) {
    this.myfragmentview=myfragmentview;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
        Log.d("debugging","mIcon11"+mIcon11.getHeight());
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    ImageView imv=(ImageView)this.myfragmentview.findViewById(R.id.imageView1);
    imv.setImageBitmap(result);
}

これが起こっていることです: ここに画像の説明を入力

1.- アプリをクリックします 2.- アプリが読み込まれます。この画面は、アプリが画像を取得する前に取得されたものです。3.- 画像を取得すると、表示されます。4.- フラグメントを次のフラグメントにスワイプします。5.- 2 番目のフラグメント 6.- 3 番目のフラグメントにもう一度スワイプします。7. 3 番目のフラグメント。8. 最初のフラグメントに戻ると、画像がロードされていません。明らかに、DownloadImageTask を再度呼び出すことはありません。これは、ユーザー エクスペリエンスが非常に遅くなるためです。

画像を保存するにはどうすればよいですか?

あ、ちなみに。1 番目から 2 番目にスワイプしただけでは、画像は「アンロード」されず、3 番目以降に移動すると発生します。なぜこれが起こっているのですか?私はちょうどこれについて興味があります。

ありがとうございました!

4

2 に答える 2

1

LruCache を使用して、ビットマップの RAM キャッシュを作成します。

この優れた Google IO トークでは、その使用方法を正確に説明しています: http://youtu.be/gbQb1PVjfqM?t=5m

于 2013-01-11T13:57:13.833 に答える
0

I found out a way that it's working:

What we've to do, is creating a variable on the fragment to know if we downloaded, and another to save the bitmap at all, something like:

int downloaded;
Bitmap pictureSaved;

Then we initialize it on "onAttach" method from fragment.

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.downloaded=0;
}

And after that, on the "main code":

if (this.downloaded==0){
//we're here if we didn't download yet
            new DownloadImageTask(myFragmentView, this).execute("http://192.168.1.33/testing/fotos/foto1.jpg");
            this.downloaded=1;
        } else if(this.downloaded==1) {
//we're here if we already downloaded.
            ImageView imv=(ImageView)myFragmentView.findViewById(R.id.imageView1);
            imv.setImageBitmap(this.pictureSaved);
        }

DownloadImageTask code, should download the image, and as you can see, I pass to his constructor "this", so I can access the fragment methods and variables.

I create a method into the fragment to update the picture:

public void update(Bitmap updated){
    ImageView imv=(ImageView)myFragmentView.findViewById(R.id.imageView1);
    this.pictureSaved=updated;
    imv.setImageBitmap(this.pictureSaved);
    this.downloaded=1;
}

DownloadImageTask calls fragment's method like this. Variables:

MyFragment frag;

Constructor:

public DownloadImageTask(View myfragmentview, MyFragmentA parent) {
    this.myfragmentview=myfragmentview;
    this.frag=parent;
}

PostExecute:

protected void onPostExecute(Bitmap result) {
    frag.update(result);
}

It works perfectly.

Hope this helps someone.

于 2013-01-13T16:11:31.590 に答える