Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2.  
  3. private static final String TAG = "MainActivity";
  4. private Button button, buttonCompress;
  5. private String encoded_string, image_name;
  6. private Bitmap bitmap;
  7. private ImageView img;
  8. private File file;
  9. private Uri file_uri;
  10. private ProgressDialog mProgressDialog;
  11. Intent data;
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. ActivityCompat.requestPermissions(this, new String[]
  18. {android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);//For writing into internal storage.
  19.  
  20. img = (ImageView) findViewById(R.id.imageView);
  21. button = (Button) findViewById(R.id.btnCam);
  22. buttonCompress = (Button) findViewById(R.id.btnComp);
  23. button.setOnClickListener(this);
  24. buttonCompress.setOnClickListener(this);
  25.  
  26. }
  27.  
  28. @Override
  29. public void onClick(View view) {
  30. switch (view.getId()) {
  31. case R.id.btnCam:
  32. Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  33. getFileUri();
  34. intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
  35. startActivityForResult(intentCamera, 10);
  36. break;
  37. case R.id.btnComp:
  38. getImageBytes(img);
  39. getFileUri();
  40. break;
  41. }
  42.  
  43. }
  44.  
  45. @Override
  46. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  47.  
  48. if (requestCode == 10 && resultCode == RESULT_OK) {
  49. img.setImageURI(file_uri);
  50. Log.d("path is", file.getPath());
  51. Toast.makeText(MainActivity.this, "Successfully Written !!", Toast.LENGTH_SHORT).show();
  52. }
  53. }
  54.  
  55. private void getFileUri() {
  56. image_name = "picture1.jpg";
  57. file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
  58. + File.separator + image_name
  59. );
  60. file_uri = Uri.fromFile(file);
  61. Log.d("uri is", file_uri.toString());
  62. }
  63.  
  64.  
  65. private byte[] getImageBytes(@NonNull ImageView imageView) {
  66. Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
  67. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  68. bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
  69. return bos.toByteArray();
  70. }
  71.  
  72. <ImageView
  73. android:id="@+id/imageView"
  74. android:layout_width="257dp"
  75. android:layout_height="300dp"
  76. app:srcCompat="@mipmap/ic_launcher"
  77. tools:layout_editor_absoluteX="32dp"
  78. tools:layout_editor_absoluteY="98dp"
  79. android:adjustViewBounds="true"
  80. android:layout_alignParentTop="true"
  81. android:layout_centerHorizontal="true" />
  82.  
  83. <Button
  84. android:id="@+id/btnCam"
  85. android:layout_width="94dp"
  86. android:layout_height="36dp"
  87. android:text="Camera"
  88. tools:layout_editor_absoluteY="448dp"
  89. tools:layout_editor_absoluteX="145dp"
  90. android:layout_marginLeft="31dp"
  91. android:layout_marginStart="31dp"
  92. android:layout_below="@+id/imageView"
  93. android:layout_alignParentLeft="true"
  94. android:layout_alignParentStart="true"
  95. android:layout_marginTop="60dp" />
  96.  
  97. <Button
  98. android:id="@+id/btnComp"
  99. android:layout_width="76dp"
  100. android:layout_height="41dp"
  101. android:text="compress"
  102. tools:layout_editor_absoluteX="-9dp"
  103. tools:layout_editor_absoluteY="-5dp"
  104. android:layout_alignBottom="@+id/btnCam"
  105. android:layout_alignRight="@+id/imageView"
  106. android:layout_alignEnd="@+id/imageView" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement