使用后台进程的广播接收器

时间:2014-04-03 07:03:08

标签: android

如何在后台进程中使用广播接收器将图像从一个文件夹复制到SD卡或本地存储中的另一个文件夹。 我试过这个link

这是我的代码:

public class Photoimport extends Activity {
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photoimport);
    button = (Button) findViewById(R.id.photoimport);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // File storageDir = new File("/storagescfsf/emulated/0/demo/");
            File rootsd = Environment.getExternalStorageDirectory();
            File srcFolder = new File(rootsd.getAbsolutePath()
                    + "/Imported/");

            if (srcFolder != getAbsolutePath()) {
                // Toast.makeText(getApplicationContext(),"Found:" + "\n" +
                // rootsd + "/import/", 1000).show();

                // File srcFolder = new File("/mnt/sdcard/Imported");
                // Check list of images //
                String files[] = srcFolder.list();
                try {
                    if (files.length <= 0) {
                        // System.out.println("No Images found");
                        Toast.makeText(
                                getApplicationContext(),
                                "No Images Found at " + srcFolder
                                        + "\n Please import and try again.",
                                1000).show();
                        finish();
                        return;

                    }

                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                    // error finish activity
                    System.exit(0);
                }
                // if (files.length <= 0) {
                // //System.out.println("No Images found");
                // Toast.makeText(getApplicationContext(),
                // "No Images Found at "+srcFolder+"\n Please import and try again.",
                // 1000)
                // .show();
                // return;
                // }

                File Photosync_dir = new File(rootsd.getAbsolutePath()
                        + "/Photosync");

                if (!Photosync_dir.exists()) {
                    if (Photosync_dir.mkdir()) {
                        // directory is created;
                        // Toast.makeText(getApplicationContext(),"Folder Created",
                        // 1000).show();
                    } else {
                        Toast.makeText(
                                getApplicationContext(),
                                "Unable to create Folder at"
                                        + Photosync_dir.getAbsolutePath(),
                                1000).show();
                        return;
                    }
                }

                // Toast.makeText(getApplicationContext(),direct +
                // " exists", 1000).show();

                Date d = new Date();
                CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss",
                        d.getTime());
                File dir = new File(Photosync_dir + "/" + s);
                if (!dir.exists()) {
                    if (dir.mkdirs()) {
                        System.out.println("newFolder created");
                    } else {
                        System.out.println("newFolder is not created");
                    }

                    File destFolder = new File(Photosync_dir
                            .getAbsolutePath() + "/" + s);

                    // make sure source exists
                    if (!srcFolder.exists()) {

                        System.out.println("Directory does not exist.");
                        // just exit
                        System.exit(0);

                    } else {

                        try {
                            cutFolder(srcFolder, destFolder);
                        } catch (IOException e) {
                            e.printStackTrace();
                            // error, just exit
                            System.exit(0);
                        }
                    }
                    // DeleteRecursive(dcim);
                    // deleteFiles(dcim.getAbsolutePath());
                    System.out.println("Done");
                    Toast.makeText(
                            getApplicationContext(),
                            files.length
                                    + " file(s) have been imported to "
                                    + destFolder.getAbsolutePath(), 1000)
                            .show();
                }

            } else {
                Toast.makeText(getApplicationContext(),
                        "Directory Not Found at " + srcFolder, 1000).show();
            }
        }

        private File getAbsolutePath() {
            // TODO Auto-generated method stub
            return null;
        }

    });
}

void DeleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        for (File child : fileOrDirectory.listFiles())
            DeleteRecursive(child);

    if (fileOrDirectory.delete()) {
        System.out.println("deleted");
    }
}

public static void deleteFiles(String path) {

    File file = new File(path);

    if (file.exists()) {
        String deleteCmd = "rm -r " + path;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(deleteCmd);
        } catch (IOException e) {
        }
    }
}

public static void cutFolder(File src, File dest) throws IOException {

    if (src.isDirectory()) {

        // if directory not exists, create it
        if (!dest.exists()) {
            dest.mkdir();
            System.out.println("Directory copied from " + src + "  to "
                    + dest);

        }

        // list all the directory contents
        String files[] = src.list();

        for (String file : files) {
            // construct the src and dest file structure
            File srcFile = new File(src, file);
            File destFile = new File(dest, file);
            // recursive copy
            cutFolder(srcFile, destFile);
        }

    } else {
        // if file, then copy it
        // Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        int length;
        // copy the file content in bytes
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();

        deleteFiles(src.getAbsolutePath());
        // boolean deleted = src.getAbsoluteFile().delete();

        // if (deleted){
        // System.out.println("file has been deleted from "+src.getAbsolutePath());
        // }
        // src.delete();
        // src.renameTo(dest);

    }
}
}

0 个答案:

没有答案