防止再次下载现有文件

时间:2014-08-09 17:52:47

标签: android android-sdcard android-file

我正在开发一个简单的应用程序,我正在使用Picasso Library从URl下载图像。

但是我有一次又一次下载同一个文件的问题。当我运行这个应用程序时,它会下载已存在的同一文件。

它会更改文件名,但文件始终相同。

这是我的代码:

package com.example.imagedownloadsample;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.squareup.picasso.Callback.EmptyCallback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

public class MainActivity extends ActionBarActivity {

    String currentUrl = "http://8020.photos.jpgmag.com/3456318_294166_528c960558_m.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_download);

        final ImageView img = (ImageView) (findViewById(R.id.imageView1));
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.loading);

        Picasso.with(this).load(currentUrl).error(R.drawable.error_detail)
                .into(img, new EmptyCallback() {
                    @Override
                    public void onSuccess() {
                        progressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError() {
                        progressBar.setVisibility(View.GONE);
                    }
                });

        Picasso.with(this).load(currentUrl).into(target);

    }

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    Random generator = new Random();
                    int n = 10000;
                    n = generator.nextInt(n);
                    String fname = "/Android/data/com.usd.pop/Image-" + n
                            + ".jpg";

                    File file = new File(Environment
                            .getExternalStorageDirectory().getPath() + fname);
                    if (file.exists())
                        file.delete();
                    try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 100, ostream);
                        ostream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {
            }
        }
    };

}

我试过这个但没有用。

                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "/Android/data/com.usd.pop/image-" + n
                        + ".jpg";

                File file = new File(Environment
                        .getExternalStorageDirectory().getPath() + fname);
                if (file.exists()) {
                    file.delete();
                }
                try {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(CompressFormat.JPEG, 100, ostream);
                    ostream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

2 个答案:

答案 0 :(得分:0)

if (file.exists())
file.delete();

上述条件将始终返回false,因为您每次都会生成随机文件名。

另外,我没有看到任何代码要更改currentUrl,因此即使您多次运行,也会使用不同的文件名下载相同的文件

答案 1 :(得分:0)

从此代码更改:

Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "/Android/data/com.usd.pop/image-" + n
                    + ".jpg";

            File file = new File(Environment
                    .getExternalStorageDirectory().getPath() + fname);
            if (file.exists()) {
                file.delete();

此代码:

String fileName = currentUrl.substring(
                        currentUrl.lastIndexOf('/') + 1,
                        currentUrl.length());

                String fname = "/Android/data/com.usd.pop/image-"
                        + fileName;

                File file = new File(Environment
                        .getExternalStorageDirectory().getPath() + fname);
                if (file.exists())
                    file.delete();

这会将文件保存为与URL文件名相同的名称