2017-02-12 15 views
0

ImageviewとButtonを使用しています。テキストをQodから変換してImageViewに表示しています。ボタン(保存)をクリックすると、その特定の画像をSDカードに保存する必要があります。これを行う方法?ボタンをクリックしてSDカードに画像を保存する方法

注:生成されたイメージは保存する必要があります。

@Override 
public class GeneratorActivity extends AppCompatActivity { 
EditText text; 
Button gen; 
ImageView image; 
String text2Qr; 
static final int REQUEST_IMAGE_CAPTURE = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_generator); 
    text = (EditText) findViewById(R.id.text); 
    gen = (Button) findViewById(R.id.gen); 
    image = (ImageView) findViewById(R.id.image); 
    gen.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      text2Qr = text.getText().toString().trim(); 
      MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); 
      try{ 
       BitMatrix bitMatrix = multiFormatWriter.encode(text2Qr, BarcodeFormat.QR_CODE,200,200); 
       BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); 
       Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix); 
       image.setImageBitmap(bitmap); 
      } 
      catch (WriterException e){ 
       e.printStackTrace(); 
      } 
     } 
    }); 

    } 

} 

}ここで

があるxmlファイル

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_generator" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.steven.qrcodegenerator.GeneratorActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="QR Code Generator"/> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="50dp" 
    android:hint="Enter Text to generate" 
    android:id="@+id/text"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="100dp" 
    android:layout_centerHorizontal="true" 
    android:text="GENERATE" 
    android:id="@+id/gen"/> 
<View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:layout_marginTop="155dp" 
    android:background="@android:color/black" 
    android:id="@+id/view" /> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="500dp" 
    android:layout_below="@+id/view"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/image"/> 
</LinearLayout> 

<Button 
    android:text="SAVE IMAGE" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button" 
    android:layout_alignBaseline="@+id/gen" 
    android:layout_alignBottom="@+id/gen" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_marginRight="19dp" 
    android:layout_marginEnd="19dp" /> 
    </RelativeLayout> 
+0

可能な重複(http://stackoverflow.com/questions/9078715/how-to-save-a-bitmap-image-with-imageview-onclick [ImageViewの持つビットマップイメージをonclickを保存する方法] ) –

+0

何か試しましたか?オンラインで調査しましたか? –

答えて

0

変更SD card.Thisコード内の画像のための保存あなたのコードは、ギャラリーに新しいフォルダを作成します。ここでは

は、メインのアクティビティコードであります。

private static String extStorageDirectory = Environment 
        .getExternalStorageDirectory().toString(); 
      private static String newFolder = "/YourGallery"; 


     gen.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

       saveImage(); 

       } 
      }); 

      } 

      @SuppressLint("SimpleDateFormat") 
      public void saveImage() throws IOException { 

       String _location; 

       if (isSdPresent()) { 
        File folder = new File(extStorageDirectory + newFolder); 
        if (!folder.exists()) { 
         folder.mkdirs(); 
        } 
        _location = extStorageDirectory + newFolder; 
       } else { 
        try { 
         File myNewFolder = new File(getFilesDir() + newFolder); 
         if (!myNewFolder.exists()) 
          myNewFolder.mkdir(); 
        } catch (Exception e1) { 
         Toast.makeText(getLocalContext(), 
           "Desire folder in sd card not found", Toast.LENGTH_LONG) 
           .show(); 
         e1.printStackTrace(); 
        } 

        _location = getFilesDir() + newFolder; 
       } 
       Calendar currentDate = Calendar.getInstance(); 
       SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMMddHmmss"); 
       String dateNow = formatter.format(currentDate.getTime()); 
       File file = new File(_location + "/" + dateNow + ".9.JPEG"); 

       FileOutputStream fos; 
       try { 
        fos = new FileOutputStream(file); 

        image.setDrawingCacheEnabled(true); 


        Bitmap bitmap = Bitmap.createBitmap(image.getDrawingCache()); 
        image.setDrawingCacheEnabled(false); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
        fos.close(); 

        MediaScannerConnection.scanFile(this, 
          new String[]{file.toString()}, null, 
          new MediaScannerConnection.OnScanCompletedListener() { 
           public void onScanCompleted(String path, Uri uri) { 
            Log.i("ExternalStorage", "Scanned " + path + ":"); 
            Log.i("ExternalStorage", "-> uri=" + uri); 

            /* Toast.makeText(getLocalContext(),"Scanned " + path + ":",Toast.LENGTH_LONG); 
            Toast.makeText(getLocalContext(),"-> uri=" + uri,Toast.LENGTH_LONG);*/ 


           } 
          }); 

        Toast.makeText(getLocalContext(), "Successfully saved", 
          Toast.LENGTH_LONG).show(); 

       } catch (FileNotFoundException e) { 
        Toast.makeText(getLocalContext(), "Error", Toast.LENGTH_LONG) 
          .show(); 

       } 

      } 

    public static boolean isSdPresent() { 
     return Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED); 
    } 
関連する問題