2013-03-27 18 views
28

đây là mã của tôi Tôi và tôi muốn lưu bitmap này vào bộ nhớ trong của mình. Công cụ boolean saveImageToInternalStorage là một mã từ google nhưng tôi không biết cách sử dụng nó. khi tôi chạm vào button2, hãy làm theo hành động button1.Cách lưu bitmap vào bộ nhớ trong

public class MainActivity extends Activity implements OnClickListener { 
Button btn, btn1; 
SurfaceView sv; 
Bitmap bitmap; 
Canvas canvas; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn=(Button)findViewById(R.id.button1); 
    btn1=(Button)findViewById(R.id.button2); 
    sv=(SurfaceView)findViewById(R.id.surfaceView1); 

    btn.setOnClickListener(this); 
    btn1.setOnClickListener(this); 

    bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 

} 
@Override 
public void onClick(View v) { 
    canvas=sv.getHolder().lockCanvas(); 
    if(canvas==null) return; 
    canvas.drawBitmap(bitmap, 100, 100, null); 
    sv.getHolder().unlockCanvasAndPost(canvas); 


} 

public boolean saveImageToInternalStorage(Bitmap image) { 

    try { 
    // Use the compress method on the Bitmap object to write image to 
    // the OutputStream 
    FileOutputStream fos = openFileOutput("desiredFilename.png", Context.MODE_PRIVATE); 

    // Writing the bitmap to the output stream 
    image.compress(Bitmap.CompressFormat.PNG, 100, fos); 
    fos.close(); 

    return true; 
    } catch (Exception e) { 
    Log.e("saveToInternalStorage()", e.getMessage()); 
    return false; 
    } 
    } 
} 

Trả lời

90

Để Tiết kiệm bitmap của bạn trong sdcard sử dụng đoạn mã sau

Image Store

private void storeImage(Bitmap image) { 
    File pictureFile = getOutputMediaFile(); 
    if (pictureFile == null) { 
     Log.d(TAG, 
       "Error creating media file, check storage permissions: ");// e.getMessage()); 
     return; 
    } 
    try { 
     FileOutputStream fos = new FileOutputStream(pictureFile); 
     image.compress(Bitmap.CompressFormat.PNG, 90, fos); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.d(TAG, "File not found: " + e.getMessage()); 
    } catch (IOException e) { 
     Log.d(TAG, "Error accessing file: " + e.getMessage()); 
    } 
} 

Để lấy đường dẫn cho hình ảnh lưu trữ

/** Create a File for saving an image or video */ 
private File getOutputMediaFile(){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory() 
      + "/Android/data/" 
      + getApplicationContext().getPackageName() 
      + "/Files"); 

    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      return null; 
     } 
    } 
    // Create a media file name 
    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date()); 
    File mediaFile; 
     String mImageName="MI_"+ timeStamp +".jpg"; 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName); 
    return mediaFile; 
} 

EDIT Từ ý kiến ​​của bạn tôi đã chỉnh sửa quan điểm onclick trong này các chức năng button1 và button2 sẽ được thực hiện riêng rẽ.

public onClick(View v){ 

switch(v.getId()){ 
case R.id.button1: 
//Your button 1 function 
break; 
case R.id. button2: 
//Your button 2 function 
break; 
} 
} 
+0

capturedImage và mImageName cách phải được tạo? –

+0

chỉnh sửa bây giờ marius – GoCrazy

+0

cảm ơn bạn rất nhiều, nhưng button2 vẫn theo nút fucntion button1 –

0

Sửa onClick() như sau:

@Override 
public void onClick(View v) { 
    if(v == btn) { 
     canvas=sv.getHolder().lockCanvas(); 
     if(canvas!=null) { 
      canvas.drawBitmap(bitmap, 100, 100, null); 
      sv.getHolder().unlockCanvasAndPost(canvas); 
     } 
    } else if(v == btn1) { 
     saveBitmapToInternalStorage(bitmap); 
    } 
} 

Có một số cách để thực thi mà btn phải được ép trước btn1 để các bitmap được sơn trước khi bạn cố gắng để lưu nó.

tôi đề nghị ban đầu bạn vô hiệu hóa btn1, và rằng bạn kích hoạt nó khi btn được nhấp, như thế này:

if(v == btn) { 
    ... 
    btn1.setEnabled(true); 
} 
+0

thế nào phải được nhìn từ if (xem == btn) và else if (xem == btn1)? –

+0

Tôi đã chỉnh sửa "xem" -> "v". Hai câu lệnh 'if()' này xác định nút nào được nhấn bên trong hàm 'onClick()' của bạn. –

3
private static void SaveImage(Bitmap finalBitmap) { 

    String root = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    File myDir = new File(root + "/saved_images"); 
    myDir.mkdirs(); 

    String fname = "Image-"+ o +".jpg"; 
    File file = new File (myDir, fname); 
    if (file.exists()) file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
     out.flush(); 
     out.close(); 

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