0

画像を含むグリッドがあります。画像をクリックすると、本のダウンロードが始まります。ダウンロードが進行中で、その画像をクリックしている場合は、ダウンロードのみが開始されます。このため、結果のファイルは破損しています。画像のonClickイベントにダウンロードタスクが実行されました。最初の画像をクリック可能にfalseにしてから、setEnablefalseにしようとしました。しかし、どちらの方法も私にはうまくいきませんでした。ダウンロードが進行中であっても、画像のクリックは機能しています。ご案内ください。詳細については、ここに私のコードがあります:

   public class GridViewAdapter extends BaseAdapter {
    ImageView imageView;
    private Context context;
    private int item;
    private LayoutInflater layoutInflater;

    GridViewAdapter(Context c, int griditem) {
        this.context = c;
        this.item = griditem;
        this.layoutInflater = (LayoutInflater) c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        return bank.size();
    }

    @Override
    public Object getItem(int position) {
      return bank.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        View grid;

        Display display = ((WindowManager) context
                .getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

        int orientation = display.getOrientation();

        if (convertView == null) {
            grid = new View(context);
            grid = layoutInflater.inflate(item, null);
        } else {
            grid = (View) convertView;
        }

        final TextView title = (TextView) grid.findViewById(R.id.mgntitle);
        title.setText(bank.get(position).getTitle());
        imageView = (ImageView) grid.findViewById(R.id.thumbnail);
        imageView.setImageResource(R.drawable.icon);
        final ProgressBar progress = (ProgressBar) grid
                .findViewById(R.id.progress);
        final ImageView downloadmark = (ImageView) grid
                .findViewById(R.id.downloadmark);
        String pdfLink = bank.get(position).getPdfLink();
        String filename = pdfLink.substring(pdfLink.lastIndexOf("/") + 1);

        final File targetDir = new File(fileLocation + filename);

        if(targetDir.exists()){
            downloadmark.setVisibility(View.VISIBLE);

        }
        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!targetDir.exists()) {
                    if (isInternetConnected()) {
                        map.put(bank.get(position).getTitle(), progress);
                        new DownloadPdfFile(GridDisplayActivity.this, bank
                                .get(position).getPdfLink(), progress,
                                downloadmark, imageView).execute();
                    } else {
                        setAlertBox("You are not connected to Internet. Please switch your connection ON to be able to Downlaod Bespoken Magazines");
                    }
                } else {

                     Toast.makeText(context, "File exists",
                            Toast.LENGTH_LONG).show();

                    // Book opening intent has to be fried here
                }
            }

        });



        imageView.setImageBitmap(BitmapFactory.decodeByteArray(
                bank.get(position).getCoverPages(), 0, bank.get(position)
                        .getCoverPages().length));

        if (!getPrefName(filename).equalsIgnoreCase("NA")) {
            if (new File(fileLocation + filename).exists()) {
                // tag.setImageResource(R.drawable.bookmark);
                 downloadmark.setVisibility(View.VISIBLE);

            } else {
                SharedPreferences pref = getApplicationContext()
                        .getSharedPreferences("BESPOKEN_PREF",
                                MODE_WORLD_WRITEABLE);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString(filename, "NA");
                editor.commit();

            }
        }

        if (new File(fileLocation + filename).exists()) {
            if (!getPrefName(filename).equalsIgnoreCase("NA")) {
                // tag.setImageResource(R.drawable.bookmark);
            //  downloadmark.setVisibility(View.VISIBLE);

            } else {

            }

        }

        return grid;

    }
}

private String getPrefName(String name) {
    SharedPreferences pref = this.getSharedPreferences("BESPOKEN_PREF",
            MODE_WORLD_WRITEABLE);
    return pref.getString(name, "NA");
}

private void saveBookName(String title) {
    SharedPreferences pref = this.getSharedPreferences("BESPOKEN_PREF",
            MODE_WORLD_WRITEABLE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString(title, title);
    editor.commit();
}

private boolean isInternetConnected() {
    connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return mWifi.isConnected() || mMobile.isConnected();
}

private class DownloadPdfFile extends AsyncTask<Void, Integer, Void> {

    private int progressupdate;
    String link;
    ProgressBar progress;
    ImageView cover;
    String file;
    ImageView dwnloadmark;

    public DownloadPdfFile(GridDisplayActivity activity, String link,
            ProgressBar progressbar, ImageView dwnmrk, ImageView imageView) {
        imageView.setEnabled(false);
        progress = progressbar;
        this.link = link;
        this.cover = imageView;
        this.dwnloadmark = dwnmrk;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        cover.setEnabled(false);
        progress.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {


        file = link.substring(link.lastIndexOf("/") + 1);
        InputStream is = null;
        long startTime = 0;
        int count;
        // DownloadFromUrl(link, fileLocation + file,progress);
        try {

            URL url = new URL(link);
            File dir = new File(fileLocation);
            if (dir.exists()) {

            } else {
                Log.i("",
                        "---------targetDir not .exists()----------------4");
                dir.mkdirs();
            }
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(fileLocation + file);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;

                publishProgress((int) ((total * 100) / lenghtOfFile));

                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();

        }
        Log.d("ImageManager",
                "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... prog) {
        progressupdate = prog[0];
        progress.setProgress(progressupdate);

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dwnloadmark.setVisibility(View.VISIBLE);
        // tag.setImageResource(R.drawable.bookmark);
        progress.setVisibility(View.INVISIBLE);
        saveBookName(file);
        cover.setEnabled(true);
    }

}



class DoBackgroundTask extends CustomAsyncTask<Void, Integer, Void> {
    private static final String TAG = "DoBackgroundTask";

    private int progressupdate;
    String link;
    private ProgressBar progress;
    ImageView dwnloadmark;
    ImageView cover;
    String file;
    GridDisplayActivity activity;

    public DoBackgroundTask(GridDisplayActivity activity, String link,
            ProgressBar progressbar, ImageView dwnmrk, ImageView img) {
        super(activity);
        img.setEnabled(false);
        this.activity = activity;
        progress = progressbar;
        this.link = link;
        this.dwnloadmark = dwnmrk;
        this.cover = img;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        cover.setEnabled(false);
        progress.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onActivityDetached() {
        if (progress != null) {
             //progress.setActivated(false);
            // progress = null;
        }
    }

    @Override
    protected void onActivityAttached() {

        //progress.setActivated(true);
        progress.setVisibility(View.VISIBLE);
        progress.setProgress(progressupdate);
        // showProgressDialog();
    }

    @Override
    protected Void doInBackground(Void... params) {

        file = link.substring(link.lastIndexOf("/") + 1);
        InputStream is = null;
        long startTime = 0;
        int count;
        // DownloadFromUrl(link, fileLocation + file,progress);
        try {

            URL url = new URL(link);
            File dir = new File(fileLocation);
            if (dir.exists()) {

            } else {

                dir.mkdirs();
            }
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(fileLocation + file);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;

                publishProgress((int) ((total * 100) / lenghtOfFile));

                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();

        }
        Log.d("ImageManager",
                "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... prog) {

        progressupdate = prog[0];

        if (mActivity != null) {
            progress.setProgress(progressupdate);
        } else {
            Log.d(TAG, "Progress updated while no Activity was attached.");
        }
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dwnloadmark.setVisibility(View.VISIBLE);    
        cover.setEnabled(true);
        saveBookName(file);

        if (mActivity != null) {
            progress.setVisibility(View.INVISIBLE);
            Toast.makeText(mActivity, "AsyncTask finished",
                    Toast.LENGTH_LONG).show();
        } else {

        }
    }
}
4

1 に答える 1

4
public class GridViewAdapter extends BaseAdapter {


    private boolean _buttonPressed = false;


    .....

    imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(_buttonPressed){
               return;
            }
            _buttonPressed = true;
            .........

    ..... 


    private class DownloadPdfFile extends AsyncTask<....> {
          .....
          @Override
          protected void onPostExecute(....) {
              _buttonPressed = false;    
          } 
          ..... 
    }
}
于 2012-08-13T13:47:12.373 に答える