Android Open Source - Android-Media-Helper File Load Task






From Project

Back to project page Android-Media-Helper.

License

The source code is released under:

Apache License

If you think the Android project Android-Media-Helper 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 mobi.sherif.imageuploader;
//ww w  .  j  av  a2s.  c o  m
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import mobi.sherif.imageuploader.MediaEngine.FileCreator;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.webkit.MimeTypeMap;

class FileLoadTask extends AsyncTask<Uri, Void, String> {
  Context mContext;
  FileCreator mFileCreator;
  Exception mException;

  public FileLoadTask(Context cxt, FileCreator filecreator) {
    mContext = cxt;
    mFileCreator = filecreator;
  }

  @Override
  protected String doInBackground(Uri... params) {
    if (params == null || params.length == 0) return null;
    Uri uri = params[0];
    File f = null;
    try {
      ContentResolver cR = mContext.getContentResolver();
      MimeTypeMap mime = MimeTypeMap.getSingleton();
      // String type = mime.getExtensionFromMimeType(cR.getType(uri));
      if (cR.getType(uri) != null && mime.getExtensionFromMimeType(cR.getType(uri)) != null) {
        f = mFileCreator.createFile(mime.getExtensionFromMimeType(cR.getType(uri)));
      } else {
        f = mFileCreator.createFile(null);
      }
      InputStream is = mContext.getContentResolver().openInputStream(uri);
      OutputStream os = new FileOutputStream(f);
      byte[] buffer = new byte[1024];
      int len;
      while ( ( len = is.read(buffer) ) != -1) {
        os.write(buffer, 0, len);
      }
      os.close();
      is.close();
      return f.getAbsolutePath();
    } catch (Exception e) {
      e.printStackTrace();
      mException = e;
      if (f != null) {
        try {
          f.delete();
        } catch (Exception e2) {
        }
      }
    }
    return null;
  }
}




Java Source Code List

mobi.sherif.example.imageuploader.ActivityForFragmentSupportVideo.java
mobi.sherif.example.imageuploader.ActivityForFragmentSupport.java
mobi.sherif.example.imageuploader.ActivityForFragmentVideo.java
mobi.sherif.example.imageuploader.ActivityForFragment.java
mobi.sherif.example.imageuploader.ImageUploaderActivitySupportVideo.java
mobi.sherif.example.imageuploader.ImageUploaderActivitySupport.java
mobi.sherif.example.imageuploader.ImageUploaderActivityVideo.java
mobi.sherif.example.imageuploader.ImageUploaderActivity.java
mobi.sherif.example.imageuploader.ImageUploaderFragmentSupportVideo.java
mobi.sherif.example.imageuploader.ImageUploaderFragmentSupport.java
mobi.sherif.example.imageuploader.ImageUploaderFragmentVideo.java
mobi.sherif.example.imageuploader.ImageUploaderFragment.java
mobi.sherif.example.imageuploader.MainActivity.java
mobi.sherif.imageuploader.ActivityManager.java
mobi.sherif.imageuploader.DefaultFileCreator.java
mobi.sherif.imageuploader.DefaultImageFileCreator.java
mobi.sherif.imageuploader.DefaultVideoFileCreator.java
mobi.sherif.imageuploader.FileLoadTask.java
mobi.sherif.imageuploader.FragmentSupportYesNoDialog.java
mobi.sherif.imageuploader.FragmentYesNoDialogBuilder.java
mobi.sherif.imageuploader.FragmentYesNoDialog.java
mobi.sherif.imageuploader.MediaEngine.java