cropping image and save to specific folder in sdcard (Android)?

送分小仙女□ 提交于 2019-12-12 01:32:40

问题


I've image has been crop. I want that image save to folder (sdcard)...I've already done crop image and show to imageview, but my problem is, I don't know how saving image crop to sdcard ?

here is my code has success to crop an image..

Bundle extras = data.getExtras();

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

where I can add the code to save an image that have been cropped?


回答1:


Try out this way:

 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)
{
}

Add below permission in manifest file.

  <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"/>


来源:https://stackoverflow.com/questions/14359985/cropping-image-and-save-to-specific-folder-in-sdcard-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!