2017-06-16 5 views
0

私はギャラリーのアプリケーションに取り組んでいます。私は、内部メモリに格納されている画像を表示するビューポケベルを使用している聞いてください。私は5つのナビゲーションメニューを持っています。 今私がこれをやりたいのは、回転メニューの画像を回転させ、回転したビットマップを既存のパスに保存することです。 私はたくさんの検索をしましたが、運はありません。メニューをクリックしたときに、 : -viewpagerで画像を回転し、既存のパスにビットマップを保存する方法

enter code her 

新しいAsyncTask(){

    @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(); 
}  

答えて

0

あなたはこれらの手順を実行する必要があり、これを達成するために、これだけのコードでこれを達成することはできません。

  1. イメージを内部ストレージからアクセスしたビットマップに変換します。
  2. ビットマップを上記の "rotate()"メソッドに渡します。
  3. Rotateメソッドは、新しいビットマップを返します。最初にイメージを取得したパスから、そのビットマップをパス上の "FileOutputStream"に保存します。

あなたは、このリンクをチェックし、画像を保存するために鍬がわからない場合:

Saving and Reading Bitmaps/Images from Internal memory in Android

+0

はまだ私は見 – honda

+0

になってください、それはエラーを示すかのアプリをクラッシュする私の質問を編集した動作していませんある時点で? Log Catの結果をここに投稿してください。だから私は何が問題であるかを見ることができます。 –

関連する問題