Skip to content

Instantly share code, notes, and snippets.

@Yazon2006
Last active December 25, 2017 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yazon2006/5e5b1f87ae221f27d89a3b36750fbec4 to your computer and use it in GitHub Desktop.
Save Yazon2006/5e5b1f87ae221f27d89a3b36750fbec4 to your computer and use it in GitHub Desktop.
change photo on the fly
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name="com.example.app.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.example.app.view.main.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name="com.example.app.PhotoService" />
<receiver
android:name="com.example.app.CameraReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE" />
<action android:name="android.hardware.action.NEW_PICTURE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>
</application>
</manifest>
class CameraReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.i("INFO", "Enter BroadcastReceiver")
val cursor = context.contentResolver.query(intent.data, null, null, null, null)
cursor.moveToFirst()
val photoPath = cursor.getString(cursor.getColumnIndex("_data"))
var bmp = BitmapFactory.decodeFile(photoPath)
val matrix = Matrix()
matrix.postRotate(90F)
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.width, bmp.height, matrix, true)
val fOut: FileOutputStream
try {
fOut = FileOutputStream(photoPath)
bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut)
fOut.flush()
fOut.close()
} catch (e: Exception) {
e.printStackTrace()
} finally {
cursor.close()
Toast.makeText(context, "Rotated Photo is Saved as : " + photoPath, Toast.LENGTH_LONG).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment