【问题标题】:Problems capturing a image and saving into a ImageView [duplicate]捕获图像并保存到 ImageView 的问题 [重复]
【发布时间】:2012-05-15 09:21:51
【问题描述】:

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

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

我知道这是一个菜鸟提示,但这是我第一次接触这些东西。

谢谢

【问题讨论】:

标签: android


【解决方案1】:

此代码会将您的 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();
}

【讨论】:

  • 非常感谢,这对我正在尝试做的事情有所帮助。现在我试图在加载应用程序时将此图像放在图像视图中,我将字符串保存在 sharedpreferences 中,但我不知道如何将此字符串变成这些东西的有用资源,我可以请你多一点帮助请?谢谢
  • 优秀。请记住选择这个作为答案:-)
【解决方案2】:

我在做这样的事情:

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 是文件的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多