我正在尝试将相机访问集成到我的Android应用程序中。我的要求是预览拍摄的照片并将其存储在SD卡上。但是在SD卡上进行预览和存储可以单独工作。但是当我将两者集成到一个应用程序时它不起作用。我正在使用开发人员站点中给出的标准代码来存储在SD卡上。 这是我的代码
public void pickImage(View View) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, REQUEST_CODE);
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "Sparikko");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("Sparikko", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
我得到一个应用程序意外停止error.And我使用logcat找到我可以找到错误发生的程序控件的位置。我发现错误发生在onActivityResult()的InputStream stream = getContentResolver().openInputStream(data.getData());
行函数。当函数组合时,它们可以单独运行,并且给出错误。
答案 0 :(得分:1)
从相机拍摄照片后,您需要缩小位图以在ImageView上设置以显示预览。参考以下方法:
private void showAttachedImagePreview(String imagePath)
{
Bitmap bitmap = null;
//= BitmapFactory.decodeFile(imagePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
try
{
bitmap = BitmapFactory.decodeFile(imagePath, options);
}
catch (OutOfMemoryError e)
{
e.printStackTrace();
bitmap=null;
try
{
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeFile(imagePath, options);
}
catch (OutOfMemoryError e1)
{
e1.printStackTrace();
bitmap=null;
try
{
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imagePath, options);
}
catch (OutOfMemoryError e2)
{
e2.printStackTrace();
bitmap=null;
try
{
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(imagePath, options);
}
catch (OutOfMemoryError e3)
{
e3.printStackTrace();
bitmap=null;
}
}
}
}
if(bitmap!=null)
{
commentAttachedImagePreview.setVisibility(ImageView.VISIBLE);
commentAttachedImagePreview.setImageBitmap(bitmap);
commentAttachedImageName.setVisibility(TextView.VISIBLE);
commentAttachedImageName.setText(new File(imagePath).getName());
deleteAttachedImage.setVisibility(ImageView.VISIBLE);
}
try
{
File file = new File(AppManager.getInstance().DATA_DIRECTORY, "tausif_tmp.png");
FileOutputStream outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.flush();
outStream.close();
filePath = file.getPath();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}