Android Open Source - Pyazing Upload Media Service






From Project

Back to project page Pyazing.

License

The source code is released under:

Apache License

If you think the Android project Pyazing 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 net.ichigotake.pyazing;
//from ww  w  . ja v a  2  s  . c  o m
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.io.FileNotFoundException;
import java.io.InputStream;

public final class UploadMediaService extends Service {

    private static final String EXTRA_MEDIA_URI = "media_uri";
    private final String LOG_TAG = UploadMediaService.class.getSimpleName();

    public static Intent createIntent(Context context, Uri data, String mimeType) {
        Intent intent = new Intent(context, UploadMediaService.class);
        intent.setType(mimeType);
        intent.putExtra(EXTRA_MEDIA_URI, data);
        return intent;
    }

    private UploadingNotification uploadingNotification;

    public UploadMediaService() {
        super();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        uploadingNotification = new UploadingNotification(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            Uri data = intent.getParcelableExtra(EXTRA_MEDIA_URI);
            try {
                upload(new UploadMedia(data, intent.getType()));
            } catch (FileNotFoundException e) {
                Log.e(LOG_TAG, "", e);
                Toasts.ignoreFile(getApplicationContext());
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void upload(final UploadMedia media) throws FileNotFoundException {
        String filename;
        if (media.isVideo() && !media.hasFilenameExtension()) {
            filename = media.getData().getLastPathSegment() + ".mp4";
        } else {
            filename = media.getData().getLastPathSegment();
        }
        InputStream inputStream = getContentResolver().openInputStream(media.getData());
        RequestParams params = new RequestParams();
        UploadMode uploadMode = media.isImage() ? UploadMode.IMAGE : UploadMode.VIDEO;
        params.put(uploadMode.getParameter(), inputStream, filename, media.getMimeType());
        // TODO: ??????????? UploadMediaActivity ?????????????????????
        if (media.isImage()) {
            params.put("auto_resize", 1);
            params.put("width", 1024);
            params.put("height", 1024);
        }
        AsyncHttpClient client = new AsyncHttpClient();
        Toasts.startUploading(getApplicationContext());
        uploadingNotification.startProgress();
        client.post(this, getString(R.string.app_server_url), params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                uploadingNotification.stopProgress();
                uploadingNotification.notifyToSuccess(new String(responseBody));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                uploadingNotification.stopProgress();
                uploadingNotification.notifyToFailure(media);
            }
        });
    }

}




Java Source Code List

net.ichigotake.pyazing.ApplicationTest.java
net.ichigotake.pyazing.CopyToClipboardService.java
net.ichigotake.pyazing.PazingApplication.java
net.ichigotake.pyazing.Toasts.java
net.ichigotake.pyazing.UploadMediaActivity.java
net.ichigotake.pyazing.UploadMediaService.java
net.ichigotake.pyazing.UploadMedia.java
net.ichigotake.pyazing.UploadMode.java
net.ichigotake.pyazing.UploadingNotification.java
net.ichigotake.pyazing.WelcomeActivity.java