2013-03-27 106 views
28

這是我的代碼,我想將該位圖保存在內部存儲器上。公共布爾saveImageToInternalStorage是來自谷歌的代碼,但我不知道如何使用它。當我觸摸button2時,請按照button1操作。如何在內部存儲器上保存位圖

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; 
    } 
    } 
} 

回答

90

保存您的位圖的SD卡使用下面的代碼

店鋪形象

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

獲取路徑用於存儲圖像

/** 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; 
} 

編輯 從您的意見我已在此Button1的和按鈕2功能編輯的onclick視圖將分別進行。

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和mImageName如何創建? – 2013-03-27 15:26:07

+0

編輯現在檢查marius – GoCrazy 2013-03-27 15:29:00

+0

非常感謝你,但button2仍然按照button1 fucntion – 2013-03-27 15:32:22

0

修改onClick()如下:

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

有幾種方法來執行,以前btn1使得bitmap塗在嘗試保存之前btn必須按下。

我建議你開始禁用btn1,並啓用它,當被點擊btn,像這樣:

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

如何必須要查看從IF(圖== BTN)和否則如果(查看== BTN1)? – 2013-03-27 15:39:09

+0

我編輯過「查看」 - >「v」。這兩個'if()'語句確定在'onClick()'函數中按下哪個按鈕。 – 2013-03-27 17:01:02

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(); 
    } 
} 
相關問題