Android Open Source - FxExplorer Media Item Util






From Project

Back to project page FxExplorer.

License

The source code is released under:

Apache License

If you think the Android project FxExplorer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package info.breezes.fxmanager;
/*from   w w  w .  j a v a 2s. c om*/
import android.os.AsyncTask;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import info.breezes.fxmanager.model.MediaItem;
import info.breezes.toolkit.log.Log;

/**
 * MediaItem Operations
 * Created by Qiao on 2015/1/2.
 */
public class MediaItemUtil {
    public static int delete(boolean fall, List<MediaItem> items) {
        int deleted = 0;
        for (MediaItem item : items) {
            File file = new File(item.path);
            if (!file.delete() && !fall) {
                break;
            }
            deleted++;
        }
        return deleted;
    }

    public static boolean rename(MediaItem item, String newName) {
        File file = new File(item.path);
        return file.renameTo(new File(file.getParent() + File.separator + newName));
    }

    public static void compress(final String outFile, final OnProgressChangeListener listener, final List<MediaItem> items) {
        new AsyncTask<Void, String, Boolean>() {
            @Override
            protected void onPreExecute() {
                listener.onPreExecute();
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                listener.onPostExecute(aBoolean);
            }

            @Override
            protected void onProgressUpdate(String... values) {
                listener.onProgressChanged(values[0], 0, 0);
            }

            @Override
            protected Boolean doInBackground(Void... params) {
                FileOutputStream fos;
                try {
                    fos = new FileOutputStream(outFile);
                    ZipOutputStream zos = new ZipOutputStream(fos);
                    for (MediaItem item : items) {
                        File f = new File(item.path);
                        if (f.isFile()) {
                            zipFile("/", f, zos);
                        } else if (f.isDirectory()) {
                            zipDir("/", zos, f);
                        }
                    }
                    zos.close();
                    return true;
                } catch (Exception e) {
                    File file = new File(outFile);
                    if (file.exists()) {
                        //noinspection ResultOfMethodCallIgnored
                        file.delete();
                    }
                    Log.e(null, e.getMessage(), e);
                }
                return false;
            }

            private void zipFile(String path, File f, ZipOutputStream zos) throws IOException {
                publishProgress(f.getName());
                ZipEntry entry = new ZipEntry(path + f.getName());
                zos.putNextEntry(entry);
                copyToZip(f, zos);
                zos.closeEntry();
            }

            private void zipDir(String s, ZipOutputStream zos, File root) throws IOException {
                publishProgress(root.getName());
                File[] files = root.listFiles();
                String cDir = s + root.getName() + "/";
                for (File f : files) {
                    if (f.isFile()) {
                        zipFile(cDir, f, zos);
                    } else if (f.isDirectory()) {
                        zipDir(cDir, zos, f);
                    }
                }
            }

            private void copyToZip(File f, ZipOutputStream zos) throws IOException {
                FileInputStream inputStream = new FileInputStream(f);
                try {
                    byte[] buf = new byte[1 << 20];
                    int c;
                    while ((c = inputStream.read(buf)) != -1) {
                        if (c > 0) {
                            zos.write(buf, 0, c);
                        }
                    }
                } finally {
                    safeClose(inputStream);
                }
            }

            private void safeClose(Closeable closeable) {
                try {
                    if (closeable != null) {
                        closeable.close();
                    }
                } catch (Exception exp) {
                    Log.w(null, exp.getMessage(), exp);
                }
            }
        }.execute();
    }

    public interface OnProgressChangeListener {
        public void onPreExecute();

        public void onProgressChanged(String file, long max, long current);

        public void onPostExecute(boolean success);
    }
}




Java Source Code List

info.breezes.fx.downloader.ApplicationTest.java
info.breezes.fx.downloader.DlMainActivity.java
info.breezes.fx.editor.ApplicationTest.java
info.breezes.fx.editor.EditorMainActivity.java
info.breezes.fx.player.ApplicationTest.java
info.breezes.fx.player.MainActivity.java
info.breezes.fx.viewer.ApplicationTest.java
info.breezes.fx.viewer.BigImageView.java
info.breezes.fx.viewer.ImageUtility.java
info.breezes.fx.viewer.MainActivity.java
info.breezes.fxmanager.ApplicationTest.java
info.breezes.fxmanager.FxApplication.java
info.breezes.fxmanager.LocalFileSystemProvider.java
info.breezes.fxmanager.MainActivity.java
info.breezes.fxmanager.MediaFragment.java
info.breezes.fxmanager.MediaItemUtil.java
info.breezes.fxmanager.MediaProvider.java
info.breezes.fxmanager.MenuAdapter.java
info.breezes.fxmanager.MimeTypeMap.java
info.breezes.fxmanager.NetUtils.java
info.breezes.fxmanager.PackagesProvider.java
info.breezes.fxmanager.ScanResultActivity.java
info.breezes.fxmanager.ScannerActivity.java
info.breezes.fxmanager.SettingsActivity.java
info.breezes.fxmanager.ShellUtil.java
info.breezes.fxmanager.StorageTool.java
info.breezes.fxmanager.ThemeChooserActivity.java
info.breezes.fxmanager.android.app.QAlertDialog.java
info.breezes.fxmanager.countly.CountlyActivity.java
info.breezes.fxmanager.countly.CountlyEvent.java
info.breezes.fxmanager.countly.CountlyFragment.java
info.breezes.fxmanager.countly.CountlyUtils.java
info.breezes.fxmanager.dialog.ApkInfoDialog.java
info.breezes.fxmanager.dialog.FileInfoDialog.java
info.breezes.fxmanager.dialog.HashInfoDialog.java
info.breezes.fxmanager.model.DrawerMenu.java
info.breezes.fxmanager.model.MediaItem.java
info.breezes.fxmanager.qrcode.QrBitmapDecoder.java
info.breezes.fxmanager.service.FileService.java
net.gescobar.httpserver.Handler.java
net.gescobar.httpserver.HttpConnection.java
net.gescobar.httpserver.HttpServer.java
net.gescobar.httpserver.Request.java
net.gescobar.httpserver.Response.java