Android相机没有保存图像

时间:2013-07-08 15:41:53

标签: android android-camera

我正在尝试使用MediaStore拍照,将图像保存到特定文件夹并显示它。已创建文件夹,但图像文件不是。

public void getPicture(View view)
{
    File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/folder");
    if(!folder.exists()) {
          if(!folder.mkdirs()){
              Toast.makeText(this, "cannot create folder", Toast.LENGTH_LONG).show();      
          }
    }

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File imageFile = new File(folder, "image.jpg");

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
    startActivityForResult(cameraIntent, RESULT_CAMERA);    
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    setContentView(R.layout.activity);

    if(resultCode == Activity.RESULT_OK)
    {
        if(requestCode == RESULT_CAMERA)
        {
            Bitmap bitmap = BitmapFactory.decodeFile(REAR_END_PATH + "/image.jpg");

            // create byte array from the Bitmap object
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream);

            ImageView iv = new ImageView(this);
            iv.setImageBitmap(bitmap);
            setContentView(iv);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试我的代码文件

BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 2;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);
        poto.setImageBitmap(bitmap);

    }
    catch (NullPointerException e) 
    {
        //e.printStackTrace();
    }

}

 //------------ Helper Methods ---------------------- 

  // Creating file uri to store image/video
  public Uri getOutputMediaFileUri(int type) 
{
      return Uri.fromFile(getOutputMediaFile(type));
}

// returning image 
private static File getOutputMediaFile(int type)

{

        // External sdcard location

        File mediaStorageDir = new File
                (
                Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME
                );

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) 
        {
        if (!mediaStorageDir.mkdirs()) 
        {
        Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
            + IMAGE_DIRECTORY_NAME + " directory");
        return null;
        }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;

        if (type ==MEDIA_TYPE_IMAGE)
        {
            mediaFile = new File (mediaStorageDir.getPath()+ File.separator + "IMG_" + timeStamp + ".jpg");
        }
        else 
        {
            return null;
        }
    // TODO Auto-generated method stub
        return mediaFile;

}