2016-07-01 6 views
1

回転イメージを保存してアンドロイドデバイス内の他の場所に移動したいと思います。ビットマップイメージを回転して保存する

  1. イメージを回転してイメージビューに設定できます。
  2. 自分が選んだ目的地にUN-ROTATED画像をコピーすることができます。

私が保存されて回転した画像ファイルを取得されて行うことができません唯一のもの(rotated.jpg)

以下の私のコードが回転します(これはストレージに回転し、ファイルを保存していない?)

   Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 

       Matrix matrix = new Matrix(); 
       matrix.postRotate(getImageOrientation(filePathLocal)); 
       rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);   

       //Set Image 
       ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile); 
       ibProfile.setImageBitmap(rotatedBitmap); 

上記は、アクティビティが終了するまで一時的にしか回転しませんが、今度は回転した画像を上のコードから保存し、サーバーにアップロードする前に移動したいと思っています。ファイルのコピー/それらのコードを投稿する必要はありません - 私が必要とするのは回転画像を保存するためのコードなので、私は何かを持っています/sdcard/saved_rotated_image.jpgようINGのcreatebitmapの最後の引数にfalseを入れて

+0

ビットマップをファイルに保存する方法は知っていますか? –

答えて

2
Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 

      Matrix matrix = new Matrix(); 
      matrix.postRotate(getImageOrientation(filePathLocal)); 
      rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);   

      //Set Image 
      ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile); 
      ibProfile.setImageBitmap(rotatedBitmap); 

REFERE私のコード:

@Override 
    protected Uri doInBackground(String... params) { 
     String filepath = params[0]; 
     String filename = params[1]; 
     String filetype = params[2]; 

     Bitmap bitmap = takeScreenShot(root); 
     Matrix rotateMatrix = new Matrix(); 
     rotateMatrix.postRotate(Datas.rotationvalue); 
     Log.e("rotationvalue", Datas.rotationvalue+"..."); 
     Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, false); 

     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes); 
     try { 
      File f = new File(filepath); 
      if (!f.exists()) { 
       f.mkdir(); 
      } 
      String folderpath = f.toString(); 
      File file = new File(folderpath, filename + "." + filetype); 

      file.createNewFile(); 
      FileOutputStream fo = new FileOutputStream(file); 
      fo.write(bytes.toByteArray()); 
      fo.close(); 
      Uri uri = Uri.fromFile(file); 
      Log.e("Edited img uri", uri.toString()); 
      return uri; 
     } catch (Exception e) { 
      Log.e("Exception...occured", e.toString()); 
      e.printStackTrace(); 
      return null; 
     } 
    } 

このコードは、私自身のfine.At取り組んでいますあなたの側で試してみてください。

doc:last引数はfilterブール値です。ソース をフィルタリングする必要がある場合はtrueを返します。マトリックスに 以上の翻訳が含まれている場合にのみ適用されます。詳細は

リンク:Last argument more info

+0

おかげでBitmap.CompressFormat.PNGをBitmap.CompressFormat.JPEGに変更しなければなりませんでした。 – user3560827

+0

これでもっと詳しい情報を得ることができれば助かりました。それでは親切にupvoteしてください –

+0

私は承認しupvotedしました。干渉、私は再びそれをやるよ – user3560827

1

保存は、画像機能は次のとおりです。

\t private void saveBitmap(Bitmap bitmap, String fileName) { 
 
\t \t File file = new File(Environment.getExternalStorageDirectory(), fileName); 
 
\t \t FileOutputStream fileOS = null; 
 
\t \t try { 
 
\t \t \t fileOS = new FileOutputStream(file); 
 
\t \t \t // quality: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. 
 
\t \t \t bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOS); 
 
\t \t \t fileOS.flush(); 
 
\t \t } catch (IOException e) { 
 
\t \t \t e.printStackTrace(); 
 
\t \t } finally { 
 
\t \t \t if (fileOS != null) { 
 
\t \t \t \t try { 
 
\t \t \t \t \t fileOS.close(); 
 
\t \t \t \t } catch (IOException e) { 
 
\t \t \t \t \t e.printStackTrace(); 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t }

使用サンプル:

Bitmap bmp = BitmapFactory.decodeFile(filePathLocal); 
 

 
    Matrix matrix = new Matrix(); 
 
    matrix.postRotate(getImageOrientation(filePathLocal)); 
 
    rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);   
 

 
    saveBitmap(rotatedBitmap, "saved_rotated_image.jpg");

関連する問題