Created
May 6, 2013 23:08
-
-
Save dannyroa/5528998 to your computer and use it in GitHub Desktop.
fake camera & gallery for android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.provider.MediaStore; | |
import android.util.Log; | |
import android.widget.Toast; | |
import java.io.*; | |
public class MainActivity extends Activity { | |
final static int[] photos = {R.drawable.android_1, R.drawable.android_2, R.drawable.android_3}; | |
static int lastPhotoIndex = -1; | |
private final static String TAG = "FakeCamera"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
String action = getIntent().getAction(); | |
if (action.contains("PICK")) { | |
InputStream in = getResources().openRawResource(getNextPhoto()); | |
Bitmap bm = BitmapFactory.decodeStream(in); | |
Uri uri = null; | |
Intent result = new Intent(null, uri); | |
result.putExtra("data", bm); | |
setResult(RESULT_OK, result); | |
} | |
else { | |
prepareToSnapPicture(); | |
} | |
finish(); | |
} | |
private void prepareToSnapPicture() { | |
checkSdCard(); | |
Intent intent = getIntent(); | |
if (intent.getExtras() != null) { | |
snapPicture(intent); | |
setResult(RESULT_OK); | |
} else { | |
Log.i(TAG, "Unable to capture photo. Missing Intent Extras."); | |
setResult(RESULT_CANCELED); | |
} | |
} | |
private void checkSdCard() { | |
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { | |
Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show(); | |
Log.i(TAG, "External SD card not mounted"); | |
} | |
} | |
private void snapPicture(Intent intent) { | |
try { | |
this.copyFile(getPicturePath(intent)); | |
Toast.makeText(this, "Click!", Toast.LENGTH_SHORT).show(); | |
} catch (IOException e) { | |
Log.i(TAG, "Can't copy photo"); | |
e.printStackTrace(); | |
} | |
} | |
private File getPicturePath(Intent intent) { | |
Uri uri = (Uri) intent.getExtras().get(MediaStore.EXTRA_OUTPUT); | |
return new File(uri.getPath()); | |
} | |
private void copyFile(File destination) throws IOException { | |
InputStream in = getResources().openRawResource(getNextPhoto()); | |
OutputStream out = new FileOutputStream(destination); | |
byte[] buffer = new byte[1024]; | |
int length; | |
if (in != null) { | |
Log.i(TAG, "Input photo stream is null"); | |
while ((length = in.read(buffer)) > 0) { | |
out.write(buffer, 0, length); | |
} | |
in.close(); | |
} | |
out.close(); | |
} | |
private int getNextPhoto() { | |
if (lastPhotoIndex == photos.length - 1) { | |
lastPhotoIndex = -1; | |
} | |
return photos[++lastPhotoIndex]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment