2013-01-16 80 views
0

我的圖像已經crop.i要圖像保存到文件夾(SD卡)...我已經做過裁剪圖像並顯示到ImageView,但我的問題是,我不知道如何將圖像作物保存到SD卡 ??裁剪圖像並保存到sdcard(android)中的特定文件夾?

這裏是我的代碼有成功裁剪圖像..

Bundle extras = data.getExtras(); 

    if (extras != null) {     
     Bitmap photo = extras.getParcelable("data"); 
     mImageView.setImageBitmap(photo); 
    } 

也許有人對你能有所幫助,在那裏我可以添加代碼來保存已裁剪的圖像? 感謝..

回答

3

嘗試這個辦法:

Bundle m_extras = data.getExtras(); 
// get the cropped bitmap 
Bitmap m_thePic = m_extras.getParcelable("data"); 
String m_path = Environment.getExternalStorageDirectory().toString(); 
File m_imgDirectory = new File(m_path + "/Images/"); 
if (!m_imgDirectory.exists()) m_imgDirectory.mkdir(); 
OutputStream m_fOut = null; 
File m_file = new File(m_path); 
m_file.delete(); 
String m_fileid = System.currentTimeMillis() + ""; 
m_file = new File(m_path, "/Images/" + m_fileid + ".png"); 
try 
{ 
    if (!m_file.exists()) m_file.createNewFile(); 
    m_fOut = new FileOutputStream(m_file);  
    Bitmap m_bitmap = m_thePic.copy(Bitmap.Config.ARGB_8888, true); 
    m_bitmap.compress(Bitmap.CompressFormat.PNG, 100, m_fOut); 
    m_fOut.flush(); 
    m_fOut.close(); 
    MediaStore.Images.Media.insertImage(getContentResolver(), 
      m_file.getAbsolutePath(), m_file.getName(), m_file.getName()); 
} 
catch (Exception p_e) 
{ 
} 

添加以下權限清單文件。

<uses-permission android:name="android.permission.CAMERA" /> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature android:name="android.hardware.camera.autofocus" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
+0

謝謝......這項工作對我來說很好...... – ltvie

+0

歡迎.. :)總是樂意幫忙:) – GrIsHu