2012-10-26 192 views
0

我写的代码编辑图像,现在我要保存编辑后的图像中的SD卡要保存图像中的SD卡

image=(ImageView)findViewById(R.id.image); 
     Intent intent = getIntent(); 
     File sdCardDirectory = Environment.getExternalStorageDirectory(); 
     photo = (Bitmap) intent.getParcelableExtra("photoo"); 
     image.setImageBitmap(photo); 
+0

http:// Whatha veyoutried.com/ – njzk2

+0

所以你得到什么错误? – Lucifer

回答

4

试试这个,

void saveImage() { 

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

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

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

请亲爱的,您的答案代码只保存一张图片,但我的图片库存在我的应用程序中,我如何保存任何没有。我需要保存到SD卡的图像,谢谢 – androidqq6

+0

@ androidqq6如果你想保存许多图像然后循环它。例如,如果你有15个图像,然后使用for循环for(int i = 0; i <= 15; i ++){//用于保存图像的代码}'以保存动态图像名称。 – GoCrazy

+0

感谢您的回复我的朋友,我已经开始赏金问题关于这个问题,我用循环方法来保存图片从我的应用程序活动之一,这是无限的画廊活动,但我面临两个问题,按保存为某些形象:1-其保存相同的图像15次(同一图像)取决于你的代码:for循环for(int i = 0; i <= 15; i ++),如果你按下保存另一个图像,它不保存任何东西,请你可以继续我的赏金帖子:http://stackoverflow.com/questions/15665830/numbering-of-image-saved-from-app-resource-to-sd-card – androidqq6

0

这是很简单的。

File outpt = new File(sdCardDirectory, "photo.jpeg"); 
FileOutputStream outptOs = new FileOutputStream(outpt); 
photo.compress(Bitmap.CompressFormat.JPEG, 90, outptOs); 
1

好吧,首先,你需要给在AndroidManifest.xml下面写权限的,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

现在让我们来看看实际编写的代码您编辑的图像,

// Getting the SDCard Path 
File sdcard = Environment.getExternalStorageDirectory(); 
File editedFile = new File(sdcard, "myphoto.jpeg"); 

// if file is already exists then first delete it 
if (editedFile.exists()) 
{ 
    editedFile.delete(); 
} 

FileOutputStream fOut = new FileOutputStream(editedFile); 
photo.compress(Bitmap.CompressFormat.JPEG, 90, fOut);