在相机活动中将图像从drawable保存到sdcard
Posted
技术标签:
【中文标题】在相机活动中将图像从drawable保存到sdcard【英文标题】:Save Image from drawable to sdcard in camera activity 【发布时间】:2012-04-17 20:27:46 【问题描述】:我正在做一个项目,通过ImageView从drawable文件夹中读取图像文件,它通过成功加载
ImageView view = (ImageView) findViewById(R.id.imageView);
view.setOnTouchListener(this);
buttonTakePicture = (Button) findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener()
@Override
public void onClick(View arg0)
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback, myPictureCallback_RAW,
myPictureCallback_JPG);
现在按下takePicture按钮后,图像应该保存在sdcard中,它正在拍摄快照但不保存图像,代码如下
File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg");
FileOutputStream fos;
try
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
Toast.makeText(androidCamera.this,
"Image saved: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();
请帮我解决这个问题....在此先感谢
【问题讨论】:
【参考方案1】:您可以在 Android 中保存照片,而无需 OutputStream 和所有其他逻辑。这是一个简单的食谱,可以完成我认为您正在尝试完成的事情。请特别注意 Intent 以及如何使用它来设置图像的保存,因为我认为这是您出错的地方。
public class PhotoActivity extends Activity implements OnClickListener
private Button takePicture;
private String path;
private File imageFile;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
takePicture = (Button) findViewById(R.id.button1);
takePicture.setOnClickListener(this);
path = Environment.getExternalStorageDirectory() + "/my_image.png";
imageFile = new File(path);
@Override
public void onClick(View v)
switch (v.getId())
case R.id.button1:
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = Uri.fromFile(imageFile);
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivity(i);
break;
【讨论】:
非常有帮助和有用的答案。【参考方案2】: private static final int CAMERA_REQUEST = 1888;
buttonTakePicture.setOnClickListener(new Button.OnClickListener()
@Override
public void onClick(View arg0)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
startActivityForResult(intent, CAMERA_REQUEST);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST)
photo = (Bitmap) data.getExtras().get("data");
private Uri getImageUri()
File file1 = new File(Environment.getExternalStorageDirectory() + "/Camerafolder");
if (!file1.exists())
file1.mkdirs();
File file = new File(Environment.getExternalStorageDirectory() + "/Camerafolder/"+"img"+".png");
Uri imgUri = Uri.fromFile(file);
return imgUri;
【讨论】:
以上是关于在相机活动中将图像从drawable保存到sdcard的主要内容,如果未能解决你的问题,请参考以下文章