Guest User

Untitled

a guest
Jul 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. private void setImage(){
  2. if (loadPicture("hello", bitmap) != null) {
  3. Toast.makeText(this, "not null", Toast.LENGTH_SHORT).show();
  4. imageView.setImageBitmap(loadPicture("hello", bitmap));
  5. }
  6. }
  7.  
  8. private void takePicture(){
  9. Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
  10. File photo =
  11. new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
  12. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
  13. imageUri = Uri.fromFile(photo);
  14. startActivityForResult(intent, 0);
  15.  
  16. }
  17.  
  18. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  19. super.onActivityResult(requestCode, resultCode, data);
  20. Uri selectedImage = imageUri;
  21. getContentResolver().notifyChange(selectedImage, null);
  22.  
  23. ContentResolver cr = getContentResolver();
  24.  
  25. try {
  26. bitmap = android.provider.MediaStore.Images.Media
  27. .getBitmap(cr, selectedImage);
  28.  
  29. imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, bitmap.getHeight()/2, bitmap.getWidth()/2, false));
  30. //**Where I save the picture**
  31. savePicture("hello", bitmap, getApplicationContext());
  32.  
  33.  
  34. }
  35.  
  36. private void savePicture(String filename, Bitmap b, Context ctx){
  37. try {
  38. ObjectOutputStream oos;
  39. FileOutputStream out;// = new FileOutputStream(filename);
  40. out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
  41. oos = new ObjectOutputStream(out);
  42. b.compress(Bitmap.CompressFormat.PNG, 100, oos);
  43.  
  44. oos.close();
  45. oos.notifyAll();
  46. out.notifyAll();
  47. out.close();
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. private Bitmap loadPicture(String filename, Bitmap b){
  54. // Drawable myImage = null;
  55. try {
  56. FileInputStream fis = openFileInput(filename);
  57. ObjectInputStream ois = null;
  58. try {
  59. ois = new ObjectInputStream(fis);
  60. } catch (StreamCorruptedException e1) {
  61. e1.printStackTrace();
  62. } catch (IOException e1) {
  63. e1.printStackTrace();
  64. }
  65. // myImage = Drawable.createFromStream(ois, filename);
  66. b = BitmapFactory.decodeStream(ois);
  67. try {
  68. ois.close();
  69. fis.close();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73.  
  74. } catch (FileNotFoundException e) {
  75. e.printStackTrace();
  76. }
  77. // return myImage;
  78. return b;
  79.  
  80. }
  81.  
  82. out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
  83.  
  84. out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
Add Comment
Please, Sign In to add comment