Picset

Picset is a Camera application for Android phones with sharing preference of  Facebook  i.e user can capture an image and user have multiple option with single click event of  sharing that image on Facebook to his/her friend also user can save that captured  image as wallpaper.

Before follow this post make sure:

1. You have installed Android SDK and Eclipse and all other packages.

2. You have downloaded Facebook SDK and you have imported that library in your project. https://github.com/facebook/facebook-android-sdk

3. You have generated your hash key. For more Information check  http://developers.facebook.com/
Source Code of Picset:-
Picset.java:

package com.android.picset;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import com.facebook.android.AsyncFacebookRunner;

import com.facebook.android.DialogError;

import com.facebook.android.Facebook;

import com.facebook.android.Facebook.DialogListener;

import com.facebook.android.FacebookError;

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

import android.widget.Toast;

public class PicsetActivity extends Activity implements View.OnClickListener {

/** Called when the activity is first created. */

TabHost th;

ImageView iv;

Button ib, wall, facebook, exit;

Intent i;

static Bitmap bmp;

SharedPreferences sp;

final static int cameraData = 0;

public static Facebook fb;

public static AsyncFacebookRunner asyncRunner;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// full screen code i.e it will hide the upper panel

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.main);

initialize();

// Setting of Tab host

th.setup();

TabSpec specs = th.newTabSpec(“tag1”);

specs.setContent(R.id.Camera);

specs.setIndicator(“Camera”);

th.addTab(specs);

specs = th.newTabSpec(“tag2”);

specs.setContent(R.id.share);

specs.setIndicator(“Share”);

th.addTab(specs);

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.picset); // Here picset is a image file so don’t get confused

}

private void initialize() {

th = (TabHost) findViewById(R.id.tabhost);

iv = (ImageView) findViewById(R.id.ivReturnedPic);

ib = (Button) findViewById(R.id.bTakePic);

wall = (Button) findViewById(R.id.sWall);

facebook = (Button) findViewById(R.id.pFacbook);

exit = (Button) findViewById(R.id.exit);

ib.setOnClickListener(this);

wall.setOnClickListener(this);

facebook.setOnClickListener(this);

exit.setOnClickListener(this);

}

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

// Capturing an image

case R.id.bTakePic:

i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(i, cameraData);

break;

// Setting as Wallpaper

case R.id.sWall:

try {

getApplicationContext().setWallpaper(bmp);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

//Authorize Facebook

case R.id.pFacbook:

fb = new Facebook(“put your hash key”); // For Security purpose i’m not disclosing my own hash key so put your own Facebook hash key

// To save the password so that when user log in next time he don’t have to authenticate

sp = getPreferences(MODE_PRIVATE);

String access_token = sp.getString(“access_token”, null);

long expires = sp.getLong(“access_expires”, 0);

if (access_token != null) {

fb.setAccessToken(access_token);

}

if (expires != 0) {

fb.setAccessExpires(expires);

}

fb.authorize(PicsetActivity.this, new String[] { “user_photos,publish_checkins,publish_actions,publish_stream” }, new DialogListener() {

public void onFacebookError(FacebookError e) {

// TODO Auto-generated method stub

Toast.makeText(PicsetActivity.this, “onFacebookError”, Toast.LENGTH_SHORT).show();

}

public void onError(DialogError e) {

// TODO Auto-generated method stub

Toast.makeText(PicsetActivity.this, “onError”, Toast.LENGTH_SHORT).show();

}

public void onComplete(Bundle values) {

// TODO Auto-generated method stub

Editor editor = sp.edit();

editor.putString(“access_token”, fb.getAccessToken());

editor.putLong(“access_expires”, fb.getAccessExpires());

editor.commit();

byte[] data = null;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);

data = baos.toByteArray();

String feed = null;

Bundle params = new Bundle();

params.putString(“method”, “photos.upload”);

params.putByteArray(“picture”, data);

params.putStringArray(“description”, new String[] { feed });

// params.putString(“Description”,”Clicked by Picshare”);

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(fb);

mAsyncRunner.request(null, params, “POST”, new SampleUploadListener(), null);

Toast.makeText(PicsetActivity.this, “Successfully Shared”, Toast.LENGTH_SHORT).show();

}

public void onCancel() {

// TODO Auto-generated method stub

Toast.makeText(PicsetActivity.this, “onCancel”, Toast.LENGTH_SHORT).show();

}

});

break;

case R.id.exit:

finish();

break;

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {

bmp = (Bitmap) data.getExtras().get(“data”);

iv.setImageBitmap(bmp);

}

}

class SampleUploadListener extends BaseRequestListener {

public void onComplete(final String response, final Object state) {

try {

// process the response here: (executed in background thread)

Log.d(“Facebook-Example”, “Response: ” + response.toString());

} catch (FacebookError e) {

Log.w(“Facebook-Example”, “Facebook Error: ” + e.getMessage());

}

}

@Override

public void onFacebookError(FacebookError e, Object state) {

// TODO Auto-generated method stub

}

}

}
BaseRequestListener.java:

package com.android.picset;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.MalformedURLException;

import android.util.Log;

import com.facebook.android.AsyncFacebookRunner.RequestListener;

import com.facebook.android.FacebookError;

//Skeleton base class for RequestListeners, providing default error handling. Applications should handle these error conditions.

public abstract class BaseRequestListener implements RequestListener {

public void onFacebookError(FacebookError e, final Object state) {

Log.e(“Facebook”, e.getMessage());

e.printStackTrace();

}

public void onFileNotFoundException(FileNotFoundException e,

final Object state) {

Log.e(“Facebook”, e.getMessage());

e.printStackTrace();

}

public void onIOException(IOException e, final Object state) {

Log.e(“Facebook”, e.getMessage());

e.printStackTrace();

}

public void onMalformedURLException(MalformedURLException e,

final Object state) {

Log.e(“Facebook”, e.getMessage());

e.printStackTrace();

}

}
Layout Designing:-

main.xml:

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android= “http://schemas.android.com/apk/res/android&#8221;

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:orientation=”vertical” >

<TabHost

android:id=”@+id/tabhost”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content” >

<LinearLayout

android:id=”@+id/linearLayout1″

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:orientation=”vertical” >

<TabWidget

android:id=”@android:id/tabs”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content” />

<FrameLayout

android:id=”@android:id/tabcontent”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content” >

<LinearLayout

android:id=”@+id/Camera”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:orientation=”vertical” >

<ImageView

android:id=”@+id/ivReturnedPic”

android:layout_width=”fill_parent”

android:layout_height=”300dp”

android:layout_gravity=”top” />

<Button

android:id=”@+id/bTakePic”

android:layout_width=”60dp”

android:layout_height=”60dp”

android:layout_gravity=”center”

android:background=”@drawable/click” />

</LinearLayout>

<LinearLayout

android:id=”@+id/share”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:background=”#5E767E”

android:orientation=”vertical” >

<View

android:layout_width=”1dp”

android:layout_height=”30dp” >

</View>

<Button

android:id=”@+id/sWall”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:gravity=”center”

android:text=”@string/sWall” />

<View

android:layout_width=”1dp”

android:layout_height=”30dp” >

</View>

<Button

android:id=”@+id/pFacbook”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:gravity=”center”

android:text=”@string/sFacebook” />

<View

android:layout_width=”1dp”

android:layout_height=”30dp” >

</View>

<Button

android:id=”@+id/exit”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:gravity=”center”

android:text=”@string/exit” />

</LinearLayout>

</FrameLayout>

</LinearLayout>

</TabHost>

</LinearLayout>
string.xml:

<?xml version=”1.0″ encoding=”utf-8″?>

<resources>

<string name=”app_name”>Picset</string>

<string name=”sWall”> Set As Wallpaper</string>

<string name=”sFacebook”>Share to Facebook</string>

<string name=”exit”>Exit</string>

</resources>

AndroidManifest.xml:

Give permission of Internet, Camera, Setwallpaper to your manifest.    Permission->Add->Uses permission.

<?xml version=”1.0″ encoding=”utf-8″?>

<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;

package=”com.android.picset”

android:versionCode=”1″

android:versionName=”1.0″ >

<uses-sdk android:minSdkVersion=”7″ />

<uses-permission android:name=”android.permission.SET_WALLPAPER” />

<uses-permission android:name=”android.permission.INTERNET” />

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

<uses-permission android:name=”android.permission.CAMERA” />

<application

android:icon=”@drawable/picset”

android:label=”@string/app_name” >

<activity

android:name=”.PicsetActivity”

android:label=”@string/app_name” >

<intent-filter>

<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

<activity android:name=”.Email”

android:label=”@string/app_name”>

<intent-filter>

<action android:name=”com.jeet.EMAIL” />

<category android:name=”android.intent.category.DEFAULT” />

</intent-filter>

</activity>

</application>

</manifest>

I will wait for your  anticipated Feedback and Suggestion.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s