Open Image, Costless Depict Something On Bitmap, As Well As Salve Bitmap To Externalstorage

Posted on

Example to charge picture amongst intent of Intent.ACTION_PICK, costless depict something on the image, as well as salve the bitmap to storage.

I accept a serial representative of “Something close processing images inwards Android“, but accept exhibit how to salve the outcome bitmap to SD Card. This representative alteration from ane of the representative “Detect affect as well as costless depict on Bitmap“, add together the share to salve the outcome bitmap to External Storage, amongst file refer “test.jpg”.


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=""     xmlns:tools=""     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androiddrawbitmap.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text=""         android:textStyle="bold" />      <Button         android:id="@+id/loadimage"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Load Image" />      <Button         android:id="@+id/saveimage"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Save Image" />      <ImageView         android:id="@+id/result"         android:scaleType="centerInside"         android:adjustViewBounds="true"         android:layout_gravity="center"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@android:color/background_dark" /> </LinearLayout>

package com.blogspot.android_er.androiddrawbitmap;  import android.content.Intent; import; import; import; import; import; import; import android.os.Bundle; import android.os.Environment; import; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast;  import; import; import; import;  populace degree MainActivity extends AppCompatActivity {      Button btnLoadImage, btnSaveImage;     ImageView imageResult;      terminal int RQS_IMAGE1 = 1;      Uri source;     Bitmap bitmapMaster;     Canvas canvasMaster;      int prvX, prvY;      Paint paintDraw;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          btnLoadImage = (Button)findViewById(;         btnSaveImage = (Button)findViewById(;         imageResult = (ImageView)findViewById(;          paintDraw = novel Paint();         paintDraw.setStyle(Paint.Style.FILL);         paintDraw.setColor(Color.WHITE);         paintDraw.setStrokeWidth(10);          btnLoadImage.setOnClickListener(new View.OnClickListener() {              @Override             populace void onClick(View arg0) {                 Intent intent = novel Intent(Intent.ACTION_PICK,                         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                 startActivityForResult(intent, RQS_IMAGE1);             }         });          btnSaveImage.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 if(bitmapMaster != null){                     saveBitmap(bitmapMaster);                 }             }         });          imageResult.setOnTouchListener(new View.OnTouchListener() {              @Override             populace boolean onTouch(View v, MotionEvent event) {                  int activeness = event.getAction();                 int x = (int) event.getX();                 int y = (int) event.getY();                 switch (action) {                     representative MotionEvent.ACTION_DOWN:                         prvX = x;                         prvY = y;                         drawOnProjectedBitMap((ImageView) v, bitmapMaster, prvX, prvY, x, y);                         break;                     representative MotionEvent.ACTION_MOVE:                         drawOnProjectedBitMap((ImageView) v, bitmapMaster, prvX, prvY, x, y);                         prvX = x;                         prvY = y;                         break;                     representative MotionEvent.ACTION_UP:                         drawOnProjectedBitMap((ImageView) v, bitmapMaster, prvX, prvY, x, y);                         break;                 }     /*      * Return 'true' to signal that the lawsuit accept been consumed.      * If auto-generated 'false', your code tin bring out ACTION_DOWN only,      * cannot bring out ACTION_MOVE as well as ACTION_UP.      */                 render true;             }         });     }      /*     Project seat on ImageView to seat on Bitmap depict on it      */      person void drawOnProjectedBitMap(ImageView iv, Bitmap bm,                                        float x0, float y0, float x, float y){         if(x<0 || y<0 || x > iv.getWidth() || y > iv.getHeight()){             //outside ImageView             return;         }else{              float ratioWidth = (float)bm.getWidth()/(float)iv.getWidth();             float ratioHeight = (float)bm.getHeight()/(float)iv.getHeight();              canvasMaster.drawLine(                     x0 * ratioWidth,                     y0 * ratioHeight,                     x * ratioWidth,                     y * ratioHeight,                     paintDraw);             imageResult.invalidate();         }     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         super.onActivityResult(requestCode, resultCode, data);          Bitmap tempBitmap;          if(resultCode == RESULT_OK){             switch (requestCode){                 representative RQS_IMAGE1:                     source = data.getData();                      try {                         //tempBitmap is Immutable bitmap,                         //cannot last passed to Canvas constructor                         tempBitmap = BitmapFactory.decodeStream(                                 getContentResolver().openInputStream(source));                          Bitmap.Config config;                         if(tempBitmap.getConfig() != null){                             config = tempBitmap.getConfig();                         }else{                             config = Bitmap.Config.ARGB_8888;                         }                          //bitmapMaster is Mutable bitmap                         bitmapMaster = Bitmap.createBitmap(                                 tempBitmap.getWidth(),                                 tempBitmap.getHeight(),                                 config);                          canvasMaster = novel Canvas(bitmapMaster);                         canvasMaster.drawBitmap(tempBitmap, 0, 0, null);                          imageResult.setImageBitmap(bitmapMaster);                     } select handgrip of (FileNotFoundException e) {                         // TODO Auto-generated select handgrip of block                         e.printStackTrace();                     }                      break;             }         }     }      person void saveBitmap(Bitmap bm){         File file = Environment.getExternalStorageDirectory();         File newFile = novel File(file, "test.jpg");          try {             FileOutputStream fileOutputStream = novel FileOutputStream(newFile);             bm.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);             fileOutputStream.flush();             fileOutputStream.close();             Toast.makeText(MainActivity.this,                     "Save Bitmap: " + fileOutputStream.toString(),                     Toast.LENGTH_LONG).show();         } select handgrip of (FileNotFoundException e) {             e.printStackTrace();             Toast.makeText(MainActivity.this,                     "Something wrong: " + e.getMessage(),                     Toast.LENGTH_LONG).show();         } select handgrip of (IOException e) {             e.printStackTrace();             Toast.makeText(MainActivity.this,                     "Something wrong: " + e.getMessage(),                     Toast.LENGTH_LONG).show();         }     } }  

“android.permission.WRITE_EXTERNAL_STORAGE” is needed inwards AndroidManifest.xml. Also I add together android:largeHeap=”true” to require to a greater extent than heap for the app. But if y’all accept a actually large image, it all the same volition closed, caused past times Out Of Memory!

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=""     package="com.blogspot.android_er.androiddrawbitmap">      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>     <application         android:largeHeap="true"         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsRtl="true"         android:theme="@style/AppTheme">         <activity android:name=".MainActivity">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application>  </manifest>  

Example to charge picture amongst intent of Intent Open image, costless depict something on bitmap, as well as salve bitmap to ExternalStorageDownload the files (Android Studio Format) .

more: Something close processing images inwards Android