捕获图像并保存到ImageView时出现问题

时间:2012-05-15 09:21:51

标签: android

  

可能重复:
  Capture Image from Camera and Display in Activity

我是android编程的新手,我遇到了一些问题。 我有一个ImageView没有加载任何图像,当我用相机拍照时,捕获的图像放入ImageView。我想保存这个,但我不知道如何在一个字符串中保存照片的目录,以及如何在“onCreate”的imageView中加载这个目录。

我知道这是一个noob cuestion,但这是我第一次使用这个东西。

由于

2 个答案:

答案 0 :(得分:0)

此代码将您的ImageView作为位图

ImageView imgView = (ImageView) findViewById(R.id.myImageView);
imgView.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(imgView.getDrawingCache());

File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/your_app_package/image.png");

此代码会将其保存到文件

try {
       FileOutputStream out = new FileOutputStream(cachePath);
       bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
       e.printStackTrace();
}

答案 1 :(得分:0)

我这样做:

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.JPEG, 80, bytes);


File f = new File(Environment.getExternalStorageDirectory() + File.separator + picId + ".jpg");
FileOutputStream fo = null;
try 
{
    f.createNewFile();
    fo = new FileOutputStream(f);
} 
catch (IOException e) 
{
    e.printStackTrace();
}
catch (FileNotFoundException e) 
{
    e.printStackTrace();
}


try 
{
    fo.write(bytes.toByteArray());
} 
catch (IOException e) 
{
    e.printStackTrace();
}

...其中picId是文件的名称。