0

我正在尝试将相机访问集成到我的 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);
   }

我收到一个应用程序意外停止错误。我使用 logcat 查找在哪里可以找到发生错误的程序控件。我发现错误发生在InputStream stream = getContentResolver().openInputStream(data.getData());onActivityResult() 函数中。两种功能都可以单独工作组合时出错。

4

1 回答 1

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();
    }
}
于 2013-04-11T06:06:22.843 回答