0

I am working on a gallery kind of application.

Here, I am using view pager to display images which are stored in internal memory, and I have five bottom navigation menus.

Now what I want to do this is to rotate the image on rotate menu click and save the rotated bitmap to it existing path.

I have searched a lot but no luck.

On menu click :

new AsyncTask<Void,Void,Void>(){

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Bitmap myBitmap = BitmapFactory.decodeFile(Constant.image_paths.get(viewPager.getCurrentItem()));
            Bitmap newBit = rotate(myBitmap,45);
            saveToInternalStorage(newBit,Constant.image_paths.get(viewPager.getCurrentItem()));
        } catch (Exception e){
            Log.d("error","error in rotate");
        }
        return null;
   }

   @Override
   protected void onPostExecute(Void aVoid) {
       super.onPostExecute(aVoid);
       myViewPagerAdapter.notifyDataSetChanged();
   }

}.execute();




public static Bitmap rotate(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(),source.getHeight(), matrix, false);
}
private String saveToInternalStorage(Bitmap bitmapImage,String path) throws IOException {
    ContextWrapper cw = new ContextWrapper(getActivity());
    // path to /data/data/yourapp/app_data/imageDir
 //   File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(path);

    if(mypath.exists()){
        mypath.delete();
    }

    if(!mypath.exists()){
        mypath.createNewFile();
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return mypath.getAbsolutePath();
}    
Elydasian
  • 2,016
  • 5
  • 23
  • 41
honda
  • 5
  • 3

1 Answers1

0

You can't achieve this by just this code, to achieve this you need to follow these steps:

  1. Convert the image into Bitmap that you accessed from you internal storage.
  2. Pass that bitmap to the method "rotate()" you posted above.
  3. Rotate method will return a new bitmap, save that bitmap through "FileOutputStream" in your storage on the path from that you got the image for the first time.

If you don't know hoe to save image, check this link:

Saving and Reading Bitmaps/Images from Internal memory in Android

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11