2016-10-24 5 views
0
 save.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View b) { 
      if ((d1.getText().toString().trim().equals("")) && d2.getText().toString().trim().equals("") 
        && d3.getText().toString().trim().equals("") && d4.getText().toString().trim().equals("") && d5.getText().toString().trim().equals("") 
        && d6.getText().toString().trim().equals("")) 
      { 
       Toast.makeText(getApplicationContext(), "Generate before Save", Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(), "Your Qr Image Has Been Save in /DCIM/Camera ", Toast.LENGTH_SHORT).show(); 

       // TODO Auto-generated method stub 
       //attempt to save the image 

       b = findViewById(R.id.image); 
       b.setDrawingCacheEnabled(true); 
       Bitmap bitmap = b.getDrawingCache(); 
       File file = new File("/DCIM/Camera/"); 
       File root = Environment.getExternalStorageDirectory(); 
       File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpeg"); 
       try { 
       cachePath.createNewFile(); 
       FileOutputStream ostream = new FileOutputStream(cachePath); 

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream); 
        ostream.flush(); 
        ostream.close(); 


       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 

     } 
    }); 


    } 





} 

は私のイメージビューから別の別の画像を保存する代わりに、前の画像を上書きする方法はありますか?このコードは機能しましたが、別の名前で別のイメージを保存することはできません。誰かが私を助けることができる?アンドロイドに別の画像を保存するにはどうすればいいですか?

+0

アクティビティのコード全体を指定してください。 – grant

+0

Path.GetTempFileName()を使用してください。 –

答えて

0

あなたはちょうどそれを変更したり、最後にタイムスタンプを付加し、全ての画像File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpeg");に同じ名前を与えています。

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image" + timeStamp + ".jpeg"); 
+0

あなたのコードは実際に働いてくれてありがとうございますが、可能であれば、とにかくIMG1、IMG2、IMG3のような増分番号にタイムスタンプを変更するのですか? –

+0

私はアンドロイドの開発者に新しいですとあなたは私のコードを提供することができれば、私は感謝されるsharedpreferencesに基底クラス、または店舗のカウンターで静的なカウンタを作成し、 –

+0

\新しい画像を追加するたびに、それを増やし、あなたに非常に多くの先生に感謝。 –

0

あなたが、カスタムEDITTEXTと新しいアラートダイアログを作成

File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpeg"); 

可能

周り仕事を変えるEDITTEXTで画像の名前を保存する必要があり、それを設定します文字列値に

String renmae=editText.getText().toString(); 
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/" + renmae+ ".jpeg"); 

か、他タイムスタンプ

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image" + timeStamp + ".jpeg"); 
+0

は、あなたにそれを非常に多くの先生に感謝あなたの助けのためにどうもありがとうございました –

+0

あなたのコードは実際に働いてくれてありがとうございますが、可能であれば、とにかくIMG1、IMG2、IMG3のような増分番号にタイムスタンプを変更するのですか? –

+0

はい、データ型intの静的変数を使用して、ファイルキャッシュに追加します。 スタティックint i = 1; ファイルキャッシュパス=新しいファイル(root.getAbsolutePath()+ "/ DCIM/Camera /" + renmae + "" + i + "jpeg"); ' **保存後** change i = i ++; // 2 @HarrisFreds –

0

使用このコード使用して名前を保存します。

String imageName="image-"+System.currentTimeMillis()+".jpeg"; 

File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/"+imageName); 

すべての時間を、imagenameのは異なっています。

+0

に私をインクリメントするために、あなたのコードが実際に働いていた先生に感謝しますが、可能な場合は、IMG1、IMG2、IMG3のような増分数にタイムスタンプを変更するには、とにかくはありますか? –

+0

あなたは1つのカウンタ変数とその増分をあなたの問題を解決するために追加することができます。 – dipali

0
save.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View b) { 
      if ((d1.getText().toString().trim().equals("")) && d2.getText().toString().trim().equals("") 
        && d3.getText().toString().trim().equals("") && d4.getText().toString().trim().equals("") && d5.getText().toString().trim().equals("") 
        && d6.getText().toString().trim().equals("")) 
      { 
       Toast.makeText(getApplicationContext(), "Generate before Save", Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(), "Your Qr Image Has Been Save in /DCIM/Camera ", Toast.LENGTH_SHORT).show(); 

       // TODO Auto-generated method stub 
       //attempt to save the image 

       b = findViewById(R.id.image); 
       b.setDrawingCacheEnabled(true); 
       Bitmap bitmap = b.getDrawingCache(); 
       File file = new File("/DCIM/Camera/"); 
       File root = Environment.getExternalStorageDirectory(); 
       File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image_"+System.currentTimeMillis()+".jpeg"); 
       try { 
       cachePath.createNewFile(); 
       FileOutputStream ostream = new FileOutputStream(cachePath); 

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream); 
        ostream.flush(); 
        ostream.close(); 
     MediaStore.Images.Media.insertImage(getContentResolver(),cachePath.getAbsolutePath(),cachePath.getName(),cachePath.getName()); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 

     } 
    }); 

    } 
+0

ありがとうございます。 –

+0

IMG1、IMG2、IMG3のような増分番号にタイムスタンプを変更するには、可能であればあなたのコードが実際に働いてくれてありがとうございます。 –

+0

@HarrisFreds System.currentTimeMillis()の代わりに1つのintカウントを使用できます。 int Count = 1; "/DCIM/Camera/image_"+Count+".jpeg"); ...... Count ++; –

関連する問題